Skip to content

Commit 753ba8c

Browse files
committed
feat: add solutions to lc problem: No.1291
No.1291.Sequential Digits
1 parent d80df58 commit 753ba8c

File tree

7 files changed

+246
-2
lines changed

7 files changed

+246
-2
lines changed

solution/1200-1299/1291.Sequential Digits/README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,113 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
**方法一:枚举**
40+
41+
我们可以枚举数字的第一位 $i$,然后枚举数字的最后一位 $j$,那么这个数字就是 $i,i+1,\cdots,j$ 这 $j-i+1$ 个数字组成的。我们可以通过不断地将数字乘以 $10$ 并加上下一个数字 $j+1$ 来得到下一个数字,如果数字在 $[low, high]$ 的范围内,我们就将它加入答案中。
42+
43+
枚举结束后,我们将答案数组排序并返回即可。
44+
45+
时间复杂度近似 $O(1)$,空间复杂度 $O(1)$。
46+
3947
<!-- tabs:start -->
4048

4149
### **Python3**
4250

4351
<!-- 这里可写当前语言的特殊实现逻辑 -->
4452

4553
```python
46-
54+
class Solution:
55+
def sequentialDigits(self, low: int, high: int) -> List[int]:
56+
ans = []
57+
for i in range(1, 9):
58+
x = i
59+
for j in range(i + 1, 10):
60+
x = x * 10 + j
61+
if low <= x <= high:
62+
ans.append(x)
63+
return sorted(ans)
4764
```
4865

4966
### **Java**
5067

5168
<!-- 这里可写当前语言的特殊实现逻辑 -->
5269

5370
```java
71+
class Solution {
72+
public List<Integer> sequentialDigits(int low, int high) {
73+
List<Integer> ans = new ArrayList<>();
74+
for (int i = 1; i < 9; ++i) {
75+
int x = i;
76+
for (int j = i + 1; j < 10; ++j) {
77+
x = x * 10 + j;
78+
if (x >= low && x <= high) {
79+
ans.add(x);
80+
}
81+
}
82+
}
83+
Collections.sort(ans);
84+
return ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
vector<int> sequentialDigits(int low, int high) {
95+
vector<int> ans;
96+
for (int i = 1; i < 9; ++i) {
97+
int x = i;
98+
for (int j = i + 1; j < 10; ++j) {
99+
x = x * 10 + j;
100+
if (x >= low && x <= high) {
101+
ans.push_back(x);
102+
}
103+
}
104+
}
105+
sort(ans.begin(), ans.end());
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func sequentialDigits(low int, high int) (ans []int) {
115+
for i := 1; i < 9; i++ {
116+
x := i
117+
for j := i + 1; j < 10; j++ {
118+
x = x*10 + j
119+
if low <= x && x <= high {
120+
ans = append(ans, x)
121+
}
122+
}
123+
}
124+
sort.Ints(ans)
125+
return
126+
}
127+
```
54128

129+
### **TypeScript**
130+
131+
```ts
132+
function sequentialDigits(low: number, high: number): number[] {
133+
const ans: number[] = [];
134+
for (let i = 1; i < 9; ++i) {
135+
let x = i;
136+
for (let j = i + 1; j < 10; ++j) {
137+
x = x * 10 + j;
138+
if (x >= low && x <= high) {
139+
ans.push(x);
140+
}
141+
}
142+
}
143+
ans.sort((a, b) => a - b);
144+
return ans;
145+
}
55146
```
56147

57148
### **...**

solution/1200-1299/1291.Sequential Digits/README_EN.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,96 @@
3030
### **Python3**
3131

3232
```python
33-
33+
class Solution:
34+
def sequentialDigits(self, low: int, high: int) -> List[int]:
35+
ans = []
36+
for i in range(1, 9):
37+
x = i
38+
for j in range(i + 1, 10):
39+
x = x * 10 + j
40+
if low <= x <= high:
41+
ans.append(x)
42+
return sorted(ans)
3443
```
3544

3645
### **Java**
3746

3847
```java
48+
class Solution {
49+
public List<Integer> sequentialDigits(int low, int high) {
50+
List<Integer> ans = new ArrayList<>();
51+
for (int i = 1; i < 9; ++i) {
52+
int x = i;
53+
for (int j = i + 1; j < 10; ++j) {
54+
x = x * 10 + j;
55+
if (x >= low && x <= high) {
56+
ans.add(x);
57+
}
58+
}
59+
}
60+
Collections.sort(ans);
61+
return ans;
62+
}
63+
}
64+
```
65+
66+
### **C++**
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
vector<int> sequentialDigits(int low, int high) {
72+
vector<int> ans;
73+
for (int i = 1; i < 9; ++i) {
74+
int x = i;
75+
for (int j = i + 1; j < 10; ++j) {
76+
x = x * 10 + j;
77+
if (x >= low && x <= high) {
78+
ans.push_back(x);
79+
}
80+
}
81+
}
82+
sort(ans.begin(), ans.end());
83+
return ans;
84+
}
85+
};
86+
```
87+
88+
### **Go**
89+
90+
```go
91+
func sequentialDigits(low int, high int) (ans []int) {
92+
for i := 1; i < 9; i++ {
93+
x := i
94+
for j := i + 1; j < 10; j++ {
95+
x = x*10 + j
96+
if low <= x && x <= high {
97+
ans = append(ans, x)
98+
}
99+
}
100+
}
101+
sort.Ints(ans)
102+
return
103+
}
104+
```
39105

106+
### **TypeScript**
107+
108+
```ts
109+
function sequentialDigits(low: number, high: number): number[] {
110+
const ans: number[] = [];
111+
for (let i = 1; i < 9; ++i) {
112+
let x = i;
113+
for (let j = i + 1; j < 10; ++j) {
114+
x = x * 10 + j;
115+
if (x >= low && x <= high) {
116+
ans.push(x);
117+
}
118+
}
119+
}
120+
ans.sort((a, b) => a - b);
121+
return ans;
122+
}
40123
```
41124

42125
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> sequentialDigits(int low, int high) {
4+
vector<int> ans;
5+
for (int i = 1; i < 9; ++i) {
6+
int x = i;
7+
for (int j = i + 1; j < 10; ++j) {
8+
x = x * 10 + j;
9+
if (x >= low && x <= high) {
10+
ans.push_back(x);
11+
}
12+
}
13+
}
14+
sort(ans.begin(), ans.end());
15+
return ans;
16+
}
17+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func sequentialDigits(low int, high int) (ans []int) {
2+
for i := 1; i < 9; i++ {
3+
x := i
4+
for j := i + 1; j < 10; j++ {
5+
x = x*10 + j
6+
if low <= x && x <= high {
7+
ans = append(ans, x)
8+
}
9+
}
10+
}
11+
sort.Ints(ans)
12+
return
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public List<Integer> sequentialDigits(int low, int high) {
3+
List<Integer> ans = new ArrayList<>();
4+
for (int i = 1; i < 9; ++i) {
5+
int x = i;
6+
for (int j = i + 1; j < 10; ++j) {
7+
x = x * 10 + j;
8+
if (x >= low && x <= high) {
9+
ans.add(x);
10+
}
11+
}
12+
}
13+
Collections.sort(ans);
14+
return ans;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def sequentialDigits(self, low: int, high: int) -> List[int]:
3+
ans = []
4+
for i in range(1, 9):
5+
x = i
6+
for j in range(i + 1, 10):
7+
x = x * 10 + j
8+
if low <= x <= high:
9+
ans.append(x)
10+
return sorted(ans)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function sequentialDigits(low: number, high: number): number[] {
2+
const ans: number[] = [];
3+
for (let i = 1; i < 9; ++i) {
4+
let x = i;
5+
for (let j = i + 1; j < 10; ++j) {
6+
x = x * 10 + j;
7+
if (x >= low && x <= high) {
8+
ans.push(x);
9+
}
10+
}
11+
}
12+
ans.sort((a, b) => a - b);
13+
return ans;
14+
}

0 commit comments

Comments
 (0)