forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-integers-with-even-digit-sum.py
More file actions
49 lines (42 loc) · 979 Bytes
/
count-integers-with-even-digit-sum.py
File metadata and controls
49 lines (42 loc) · 979 Bytes
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
# Time: O(logn)
# Space: O(1)
# math
class Solution(object):
def countEven(self, num):
"""
:type num: int
:rtype: int
"""
def parity(x):
result = 0
while x:
result += x%10
x //= 10
return result%2
return (num-parity(num))//2
# Time: O(nlogn)
# Space: O(1)
# brute force
class Solution2(object):
def countEven(self, num):
"""
:type num: int
:rtype: int
"""
def parity(x):
result = 0
while x:
result += x%10
x //= 10
return result%2
return sum(parity(x) == 0 for x in xrange(1, num+1))
# Time: O(nlogn)
# Space: O(logn)
# brute force
class Solution3(object):
def countEven(self, num):
"""
:type num: int
:rtype: int
"""
return sum(sum(map(int, str(x)))%2 == 0 for x in xrange(1, num+1))