-
Notifications
You must be signed in to change notification settings - Fork 43
/
rotated-digits.py
123 lines (112 loc) · 3.64 KB
/
rotated-digits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Time: O(logn)
# Space: O(logn)
# X is a good number if after rotating each digit individually by 180 degrees,
# we get a valid number that is different from X.
# A number is valid if each digit remains a digit after rotation.
# 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other;
# 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number.
#
# Now given a positive number N, how many numbers X from 1 to N are good?
#
# Example:
# Input: 10
# Output: 4
# Explanation:
# There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
# Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
#
# Note:
# - N will be in range [1, 10000].
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79378135
class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
dmap = {"0" : "0", "1" : "1", "8" : "8", "2" : "5", "5" : "2", "6" : "9", "9" : "6"}
res = 0
for num in range(1, N + 1):
if any(x in str(num) for x in ["3", "4", "7"]):
continue
if any(x in str(num) for x in ["2", "5", "6", "9"]):
res += 1
return res
# V1'
# http://bookshadow.com/weblog/2018/02/25/leetcode-rotated-digits/
class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
ans = 0
for n in range(1, N + 1):
nset = set(map(int, str(n)))
if any(x in nset for x in (2, 5, 6, 9)):
if not any(x in nset for x in (3, 4, 7)):
ans += 1
return ans
# V2
class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
A = map(int, str(N))
invalid, diff = set([3, 4, 7]), set([2, 5, 6, 9])
def dp(A, i, is_prefix_equal, is_good, lookup):
if i == len(A): return int(is_good)
if (i, is_prefix_equal, is_good) not in lookup:
result = 0
for d in range(A[i]+1 if is_prefix_equal else 10):
if d in invalid: continue
result += dp(A, i+1,
is_prefix_equal and d == A[i],
is_good or d in diff,
lookup)
lookup[i, is_prefix_equal, is_good] = result
return lookup[i, is_prefix_equal, is_good]
lookup = {}
return dp(A, 0, True, False, lookup)
# Time: O(n)
# Space: O(n)
class Solution2(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
INVALID, SAME, DIFF = 0, 1, 2
same, diff = [0, 1, 8], [2, 5, 6, 9]
dp = [0] * (N+1)
dp[0] = SAME
for i in range(N//10+1):
if dp[i] != INVALID:
for j in same:
if i*10+j <= N:
dp[i*10+j] = max(SAME, dp[i])
for j in diff:
if i*10+j <= N:
dp[i*10+j] = DIFF
return dp.count(DIFF)
# Time: O(nlogn) = O(n), because O(logn) = O(32) by this input
# Space: O(logn) = O(1)
class Solution3(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
invalid, diff = set(['3', '4', '7']), set(['2', '5', '6', '9'])
result = 0
for i in range(N+1):
lookup = set(list(str(i)))
if invalid & lookup:
continue
if diff & lookup:
result += 1
return result