forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-numbers-with-even-number-of-digits.py
54 lines (44 loc) · 1.25 KB
/
find-numbers-with-even-number-of-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
# Time: O(nlog(logm)), n the length of nums, m is the max value of nums
# Space: O(logm)
import bisect
class Solution(object):
def __init__(self):
M = 10**5
self.__lookup = [0]
i = 10
while i < M:
self.__lookup.append(i)
i *= 10
self.__lookup.append(i)
def findNumbers(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def digit_count(n):
return bisect.bisect_right(self.__lookup, n)
return sum(digit_count(n) % 2 == 0 for n in nums)
# Time: O(nlogm), n the length of nums, m is the max value of nums
# Space: O(logm)
class Solution2(object):
def findNumbers(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def digit_count(n):
result = 0
while n:
n //= 10
result += 1
return result
return sum(digit_count(n) % 2 == 0 for n in nums)
# Time: O(nlogm), n the length of nums, m is the max value of nums
# Space: O(logm)
class Solution3(object):
def findNumbers(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sum(len(str(n)) % 2 == 0 for n in nums)