Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f383454

Browse files
feat: add solutions to lc problems: No.1215,1216 (#1875)
* No.1215.Stepping Numbers * No.1216.Valid Palindrome III
1 parent d35d2d9 commit f383454

File tree

8 files changed

+348
-32
lines changed

8 files changed

+348
-32
lines changed

‎solution/1200-1299/1215.Stepping Numbers/README.md‎

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434

3535
**方法一:BFS**
3636

37+
首先,如果 $low$ 为 0ドル,ドル那么我们需要将 0ドル$ 加入答案中。
38+
39+
接下来,我们创建一个队列 $q,ドル并将 1ドル \sim 9$ 加入队列中。然后我们不断从队列中取出元素,记当前元素为 $v,ドル如果 $v$ 大于 $high,ドル那么我们就停止搜索;如果 $v$ 在 $[low, high]$ 的范围内,那么我们将 $v$ 加入答案中。然后,我们需要将 $v$ 的最后一位数字记为 $x,ドル如果 $x \gt 0,ドル那么我们将 $v \times 10 + x - 1$ 加入队列中;如果 $x \lt 9,ドル那么我们将 $v \times 10 + x + 1$ 加入队列中。重复上述操作,直到队列为空。
40+
41+
时间复杂度 $O(10 \times 2^{\log M}),ドル空间复杂度 $O(2^{\log M}),ドル其中 $M$ 为 $high$ 的位数。
42+
3743
<!-- tabs:start -->
3844

3945
### **Python3**
@@ -104,17 +110,29 @@ class Solution {
104110
public:
105111
vector<int> countSteppingNumbers(int low, int high) {
106112
vector<int> ans;
107-
if (low == 0) ans.push_back(0);
113+
if (low == 0) {
114+
ans.push_back(0);
115+
}
108116
queue<long long> q;
109-
for (int i = 1; i < 10; ++i) q.push(i);
117+
for (int i = 1; i < 10; ++i) {
118+
q.push(i);
119+
}
110120
while (!q.empty()) {
111-
int v = q.front();
121+
long long v = q.front();
112122
q.pop();
113-
if (v > high) break;
114-
if (v >= low) ans.push_back(v);
123+
if (v > high) {
124+
break;
125+
}
126+
if (v >= low) {
127+
ans.push_back(v);
128+
}
115129
int x = v % 10;
116-
if (x) q.push(1ll * v * 10 + x - 1);
117-
if (x < 9) q.push(1ll * v * 10 + x + 1);
130+
if (x > 0) {
131+
q.push(v * 10 + x - 1);
132+
}
133+
if (x < 9) {
134+
q.push(v * 10 + x + 1);
135+
}
118136
}
119137
return ans;
120138
}
@@ -151,6 +169,38 @@ func countSteppingNumbers(low int, high int) []int {
151169
}
152170
```
153171

172+
### **TypeScript**
173+
174+
```ts
175+
function countSteppingNumbers(low: number, high: number): number[] {
176+
const ans: number[] = [];
177+
if (low === 0) {
178+
ans.push(0);
179+
}
180+
const q: number[] = [];
181+
for (let i = 1; i < 10; ++i) {
182+
q.push(i);
183+
}
184+
while (q.length) {
185+
const v = q.shift()!;
186+
if (v > high) {
187+
break;
188+
}
189+
if (v >= low) {
190+
ans.push(v);
191+
}
192+
const x = v % 10;
193+
if (x > 0) {
194+
q.push(v * 10 + x - 1);
195+
}
196+
if (x < 9) {
197+
q.push(v * 10 + x + 1);
198+
}
199+
}
200+
return ans;
201+
}
202+
```
203+
154204
### **...**
155205

156206
```

‎solution/1200-1299/1215.Stepping Numbers/README_EN.md‎

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636

3737
## Solutions
3838

39+
**Solution 1: BFS**
40+
41+
First, if $low$ is 0ドル,ドル we need to add 0ドル$ to the answer.
42+
43+
Next, we create a queue $q$ and add 1ドル \sim 9$ to the queue. Then, we repeatedly take out elements from the queue. Let the current element be $v$. If $v$ is greater than $high,ドル we stop searching. If $v$ is in the range $[low, high],ドル we add $v$ to the answer. Then, we need to record the last digit of $v$ as $x$. If $x \gt 0,ドル we add $v \times 10 + x - 1$ to the queue. If $x \lt 9,ドル we add $v \times 10 + x + 1$ to the queue. Repeat the above steps until the queue is empty.
44+
45+
The time complexity is $O(10 \times 2^{\log M}),ドル and the space complexity is $O(2^{\log M}),ドル where $M$ is the number of digits in $high$.
46+
3947
<!-- tabs:start -->
4048

4149
### **Python3**
@@ -102,17 +110,29 @@ class Solution {
102110
public:
103111
vector<int> countSteppingNumbers(int low, int high) {
104112
vector<int> ans;
105-
if (low == 0) ans.push_back(0);
113+
if (low == 0) {
114+
ans.push_back(0);
115+
}
106116
queue<long long> q;
107-
for (int i = 1; i < 10; ++i) q.push(i);
117+
for (int i = 1; i < 10; ++i) {
118+
q.push(i);
119+
}
108120
while (!q.empty()) {
109-
int v = q.front();
121+
long long v = q.front();
110122
q.pop();
111-
if (v > high) break;
112-
if (v >= low) ans.push_back(v);
123+
if (v > high) {
124+
break;
125+
}
126+
if (v >= low) {
127+
ans.push_back(v);
128+
}
113129
int x = v % 10;
114-
if (x) q.push(1ll * v * 10 + x - 1);
115-
if (x < 9) q.push(1ll * v * 10 + x + 1);
130+
if (x > 0) {
131+
q.push(v * 10 + x - 1);
132+
}
133+
if (x < 9) {
134+
q.push(v * 10 + x + 1);
135+
}
116136
}
117137
return ans;
118138
}
@@ -149,6 +169,38 @@ func countSteppingNumbers(low int, high int) []int {
149169
}
150170
```
151171

172+
### **TypeScript**
173+
174+
```ts
175+
function countSteppingNumbers(low: number, high: number): number[] {
176+
const ans: number[] = [];
177+
if (low === 0) {
178+
ans.push(0);
179+
}
180+
const q: number[] = [];
181+
for (let i = 1; i < 10; ++i) {
182+
q.push(i);
183+
}
184+
while (q.length) {
185+
const v = q.shift()!;
186+
if (v > high) {
187+
break;
188+
}
189+
if (v >= low) {
190+
ans.push(v);
191+
}
192+
const x = v % 10;
193+
if (x > 0) {
194+
q.push(v * 10 + x - 1);
195+
}
196+
if (x < 9) {
197+
q.push(v * 10 + x + 1);
198+
}
199+
}
200+
return ans;
201+
}
202+
```
203+
152204
### **...**
153205

154206
```
Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
class Solution {
2-
public:
3-
vector<int> countSteppingNumbers(int low, int high) {
4-
vector<int> ans;
5-
if (low == 0) ans.push_back(0);
6-
queue<long long> q;
7-
for (int i = 1; i < 10; ++i) q.push(i);
8-
while (!q.empty()) {
9-
int v = q.front();
10-
q.pop();
11-
if (v > high) break;
12-
if (v >= low) ans.push_back(v);
13-
int x = v % 10;
14-
if (x) q.push(1ll * v * 10 + x - 1);
15-
if (x < 9) q.push(1ll * v * 10 + x + 1);
16-
}
17-
return ans;
18-
}
1+
class Solution {
2+
public:
3+
vector<int> countSteppingNumbers(int low, int high) {
4+
vector<int> ans;
5+
if (low == 0) {
6+
ans.push_back(0);
7+
}
8+
queue<long long> q;
9+
for (int i = 1; i < 10; ++i) {
10+
q.push(i);
11+
}
12+
while (!q.empty()) {
13+
long long v = q.front();
14+
q.pop();
15+
if (v > high) {
16+
break;
17+
}
18+
if (v >= low) {
19+
ans.push_back(v);
20+
}
21+
int x = v % 10;
22+
if (x > 0) {
23+
q.push(v * 10 + x - 1);
24+
}
25+
if (x < 9) {
26+
q.push(v * 10 + x + 1);
27+
}
28+
}
29+
return ans;
30+
}
1931
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function countSteppingNumbers(low: number, high: number): number[] {
2+
const ans: number[] = [];
3+
if (low === 0) {
4+
ans.push(0);
5+
}
6+
const q: number[] = [];
7+
for (let i = 1; i < 10; ++i) {
8+
q.push(i);
9+
}
10+
while (q.length) {
11+
const v = q.shift()!;
12+
if (v > high) {
13+
break;
14+
}
15+
if (v >= low) {
16+
ans.push(v);
17+
}
18+
const x = v % 10;
19+
if (x > 0) {
20+
q.push(v * 10 + x - 1);
21+
}
22+
if (x < 9) {
23+
q.push(v * 10 + x + 1);
24+
}
25+
}
26+
return ans;
27+
}

‎solution/1200-1299/1216.Valid Palindrome III/README.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,63 @@ func max(a, b int) int {
170170
}
171171
```
172172

173+
### **TypeScript**
174+
175+
```ts
176+
function isValidPalindrome(s: string, k: number): boolean {
177+
const n = s.length;
178+
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
179+
for (let i = 0; i < n; ++i) {
180+
f[i][i] = 1;
181+
}
182+
for (let i = n - 2; ~i; --i) {
183+
for (let j = i + 1; j < n; ++j) {
184+
if (s[i] === s[j]) {
185+
f[i][j] = f[i + 1][j - 1] + 2;
186+
} else {
187+
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
188+
}
189+
if (f[i][j] + k >= n) {
190+
return true;
191+
}
192+
}
193+
}
194+
return false;
195+
}
196+
```
197+
198+
### **Rust**
199+
200+
```rust
201+
impl Solution {
202+
pub fn is_valid_palindrome(s: String, k: i32) -> bool {
203+
let s = s.as_bytes();
204+
let n = s.len();
205+
let mut f = vec![vec![0; n]; n];
206+
207+
for i in 0..n {
208+
f[i][i] = 1;
209+
}
210+
211+
for i in (0..n - 2).rev() {
212+
for j in (i + 1)..n {
213+
if s[i] == s[j] {
214+
f[i][j] = f[i + 1][j - 1] + 2;
215+
} else {
216+
f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]);
217+
}
218+
219+
if f[i][j] + k >= n as i32 {
220+
return true;
221+
}
222+
}
223+
}
224+
225+
false
226+
}
227+
}
228+
```
229+
173230
### **...**
174231

175232
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /