blob: 0ab6d4e19db3dab02ce03aa1aea1f43cb063081d (
plain)
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
|
# 386. Lexicographical Numbers
"""
given an integer 'n', return all the numbers in the range '[1, n]' sorted in
lexicographical order. you must write an algorithm that runs in O(n) time
complexity and uses O(1) extra space.
"""
class Solution(object):
def lexicalOrder(self, n):
"""
:type n: int
:rtype: List[int]
"""
ans = [1]
while len(ans) < n:
tmp = ans[-1] * 10
while tmp > n:
tmp /= 10
tmp += 1
while tmp % 10 == 0:
tmp /= 10
ans.append(tmp)
return ans
if __name__ == "__main__":
obj = Solution()
print(obj.lexicalOrder(13))
print(obj.lexicalOrder(2))
|