Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problems: No.1215,1216 #1875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions solution/1200-1299/1215.Stepping Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@

**方法一:BFS**

首先,如果 $low$ 为 $0$,那么我们需要将 $0$ 加入答案中。

接下来,我们创建一个队列 $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$ 加入队列中。重复上述操作,直到队列为空。

时间复杂度 $O(10 \times 2^{\log M})$,空间复杂度 $O(2^{\log M})$,其中 $M$ 为 $high$ 的位数。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -104,17 +110,29 @@ class Solution {
public:
vector<int> countSteppingNumbers(int low, int high) {
vector<int> ans;
if (low == 0) ans.push_back(0);
if (low == 0) {
ans.push_back(0);
}
queue<long long> q;
for (int i = 1; i < 10; ++i) q.push(i);
for (int i = 1; i < 10; ++i) {
q.push(i);
}
while (!q.empty()) {
int v = q.front();
long long v = q.front();
q.pop();
if (v > high) break;
if (v >= low) ans.push_back(v);
if (v > high) {
break;
}
if (v >= low) {
ans.push_back(v);
}
int x = v % 10;
if (x) q.push(1ll * v * 10 + x - 1);
if (x < 9) q.push(1ll * v * 10 + x + 1);
if (x > 0) {
q.push(v * 10 + x - 1);
}
if (x < 9) {
q.push(v * 10 + x + 1);
}
}
return ans;
}
Expand Down Expand Up @@ -151,6 +169,38 @@ func countSteppingNumbers(low int, high int) []int {
}
```

### **TypeScript**

```ts
function countSteppingNumbers(low: number, high: number): number[] {
const ans: number[] = [];
if (low === 0) {
ans.push(0);
}
const q: number[] = [];
for (let i = 1; i < 10; ++i) {
q.push(i);
}
while (q.length) {
const v = q.shift()!;
if (v > high) {
break;
}
if (v >= low) {
ans.push(v);
}
const x = v % 10;
if (x > 0) {
q.push(v * 10 + x - 1);
}
if (x < 9) {
q.push(v * 10 + x + 1);
}
}
return ans;
}
```

### **...**

```
Expand Down
66 changes: 59 additions & 7 deletions solution/1200-1299/1215.Stepping Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@

## Solutions

**Solution 1: BFS**

First, if $low$ is $0$, we need to add $0$ to the answer.

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.

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$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -102,17 +110,29 @@ class Solution {
public:
vector<int> countSteppingNumbers(int low, int high) {
vector<int> ans;
if (low == 0) ans.push_back(0);
if (low == 0) {
ans.push_back(0);
}
queue<long long> q;
for (int i = 1; i < 10; ++i) q.push(i);
for (int i = 1; i < 10; ++i) {
q.push(i);
}
while (!q.empty()) {
int v = q.front();
long long v = q.front();
q.pop();
if (v > high) break;
if (v >= low) ans.push_back(v);
if (v > high) {
break;
}
if (v >= low) {
ans.push_back(v);
}
int x = v % 10;
if (x) q.push(1ll * v * 10 + x - 1);
if (x < 9) q.push(1ll * v * 10 + x + 1);
if (x > 0) {
q.push(v * 10 + x - 1);
}
if (x < 9) {
q.push(v * 10 + x + 1);
}
}
return ans;
}
Expand Down Expand Up @@ -149,6 +169,38 @@ func countSteppingNumbers(low int, high int) []int {
}
```

### **TypeScript**

```ts
function countSteppingNumbers(low: number, high: number): number[] {
const ans: number[] = [];
if (low === 0) {
ans.push(0);
}
const q: number[] = [];
for (let i = 1; i < 10; ++i) {
q.push(i);
}
while (q.length) {
const v = q.shift()!;
if (v > high) {
break;
}
if (v >= low) {
ans.push(v);
}
const x = v % 10;
if (x > 0) {
q.push(v * 10 + x - 1);
}
if (x < 9) {
q.push(v * 10 + x + 1);
}
}
return ans;
}
```

### **...**

```
Expand Down
48 changes: 30 additions & 18 deletions solution/1200-1299/1215.Stepping Numbers/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
class Solution {
public:
vector<int> countSteppingNumbers(int low, int high) {
vector<int> ans;
if (low == 0) ans.push_back(0);
queue<long long> q;
for (int i = 1; i < 10; ++i) q.push(i);
while (!q.empty()) {
int v = q.front();
q.pop();
if (v > high) break;
if (v >= low) ans.push_back(v);
int x = v % 10;
if (x) q.push(1ll * v * 10 + x - 1);
if (x < 9) q.push(1ll * v * 10 + x + 1);
}
return ans;
}
class Solution {
public:
vector<int> countSteppingNumbers(int low, int high) {
vector<int> ans;
if (low == 0) {
ans.push_back(0);
}
queue<long long> q;
for (int i = 1; i < 10; ++i) {
q.push(i);
}
while (!q.empty()) {
long long v = q.front();
q.pop();
if (v > high) {
break;
}
if (v >= low) {
ans.push_back(v);
}
int x = v % 10;
if (x > 0) {
q.push(v * 10 + x - 1);
}
if (x < 9) {
q.push(v * 10 + x + 1);
}
}
return ans;
}
};
27 changes: 27 additions & 0 deletions solution/1200-1299/1215.Stepping Numbers/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function countSteppingNumbers(low: number, high: number): number[] {
const ans: number[] = [];
if (low === 0) {
ans.push(0);
}
const q: number[] = [];
for (let i = 1; i < 10; ++i) {
q.push(i);
}
while (q.length) {
const v = q.shift()!;
if (v > high) {
break;
}
if (v >= low) {
ans.push(v);
}
const x = v % 10;
if (x > 0) {
q.push(v * 10 + x - 1);
}
if (x < 9) {
q.push(v * 10 + x + 1);
}
}
return ans;
}
57 changes: 57 additions & 0 deletions solution/1200-1299/1216.Valid Palindrome III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,63 @@ func max(a, b int) int {
}
```

### **TypeScript**

```ts
function isValidPalindrome(s: string, k: number): boolean {
const n = s.length;
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}
for (let i = n - 2; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
if (s[i] === s[j]) {
f[i][j] = f[i + 1][j - 1] + 2;
} else {
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
}
if (f[i][j] + k >= n) {
return true;
}
}
}
return false;
}
```

### **Rust**

```rust
impl Solution {
pub fn is_valid_palindrome(s: String, k: i32) -> bool {
let s = s.as_bytes();
let n = s.len();
let mut f = vec![vec![0; n]; n];

for i in 0..n {
f[i][i] = 1;
}

for i in (0..n - 2).rev() {
for j in (i + 1)..n {
if s[i] == s[j] {
f[i][j] = f[i + 1][j - 1] + 2;
} else {
f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]);
}

if f[i][j] + k >= n as i32 {
return true;
}
}
}

false
}
}
```

### **...**

```
Expand Down
Loading