forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1291_Sequential_Digits
63 lines (50 loc) · 1.57 KB
/
1291_Sequential_Digits
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
Leetcode 1291: Sequential Digits
Detailed video explanation: https://youtu.be/sFt3KVGyeWw
===========================================
C++:
----
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
string digits = "123456789";
vector<int> result;
int nl = to_string(low).length();
int nh = to_string(high).length();
for(int i = nl; i <= nh; ++i){
for(int j = 0; j < 10-i; ++j){
int num = stoi(digits.substr(j, i));
if(num >= low && num <= high) result.push_back(num);
}
}
return result;
}
};
Java:
-----
class Solution {
public List<Integer> sequentialDigits(int low, int high) {
String digits = "123456789";
List<Integer> result = new ArrayList();
int nl = String.valueOf(low).length();
int nh = String.valueOf(high).length();
for(int i = nl; i <= nh; ++i){
for(int j = 0; j < 10-i; ++j){
int num = Integer.parseInt(digits.substring(j, j+i));
if(num >= low && num <= high) result.add(num);
}
}
return result;
}
}
Python3:
--------
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
digits = "123456789"
result = []
nl, nh = len(str(low)), len(str(high))
for i in range(nl, nh+1):
for j in range(0, 10-i):
num = int(digits[j: j+i])
if num >= low and num <= high: result.append(num)
return result