This commit is contained in:
parent
cd0bb8d030
commit
2cfc402fdd
1 changed files with 20 additions and 0 deletions
20
medium/lexicographical_numbers.py
Normal file
20
medium/lexicographical_numbers.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# https://leetcode.com/problems/lexicographical-numbers
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def lexicalOrder(self, n: int) -> List[int]:
|
||||
result: List[int] = []
|
||||
s: List[int] = [int(x) for x in list(str(n))]
|
||||
def iterate(curr: int, length: int, curr_index: int):
|
||||
if curr_index >= len(s):
|
||||
return
|
||||
for num in range(1 if curr_index == 0 else 0, 10):
|
||||
new_curr = (curr * 10) + num
|
||||
if new_curr > n:
|
||||
break
|
||||
result.append(new_curr)
|
||||
iterate(new_curr, length + 1, curr_index + 1)
|
||||
|
||||
iterate(0, 0, 0)
|
||||
return result
|
||||
Loading…
Add table
Add a link
Reference in a new issue