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 d3e53ba

Browse files
feat: update solutions to lc problem: No.0967 (doocs#3397)
No.0967.Numbers With Same Consecutive Differences
1 parent 794b63a commit d3e53ba

File tree

11 files changed

+284
-263
lines changed

11 files changed

+284
-263
lines changed

‎solution/0700-0799/0737.Sentence Similarity II/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tags:
3030
<p>两个句子是相似的,如果:</p>
3131

3232
<ul>
33-
<li>它们具有 <strong>相同的长度</strong> (即相同的字数)</li>
33+
<li>它们具有 <strong>相同的长度</strong> (即相同的词数)</li>
3434
<li><code>sentence1[i]</code>&nbsp;和&nbsp;<code>sentence2[i]</code>&nbsp;是相似的</li>
3535
</ul>
3636

‎solution/0900-0999/0967.Numbers With Same Consecutive Differences/README.md‎

Lines changed: 92 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:DFS
72+
73+
我们可以枚举所有长度为 $n$ 的数字的第一个数字,然后使用深度优先搜索的方法,递归地构造所有符合条件的数字。
74+
75+
具体地,我们首先定义一个边界值 $\textit{boundary} = 10^{n-1},ドル表示我们需要构造的数字的最小值。然后,我们从 1ドル$ 到 9ドル$ 枚举第一个数字,对于每一个数字 $i,ドル我们递归地构造以 $i$ 为第一个数字的长度为 $n$ 的数字。
76+
77+
时间复杂度 $(n \times 2^n \times |\Sigma|),ドル其中 $|\Sigma|$ 表示数字集合,本题中 $|\Sigma| = 9$。空间复杂度 $O(2^n)$。
7278

7379
<!-- tabs:start -->
7480

@@ -77,50 +83,51 @@ tags:
7783
```python
7884
class Solution:
7985
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
80-
ans = []
81-
82-
def dfs(n, k, t):
83-
if n == 0:
84-
ans.append(t)
86+
def dfs(x: int):
87+
if x >= boundary:
88+
ans.append(x)
8589
return
86-
last = t % 10
90+
last = x % 10
8791
if last + k <= 9:
88-
dfs(n -1, k, t * 10 + last + k)
92+
dfs(x * 10 + last + k)
8993
if last - k >= 0 and k != 0:
90-
dfs(n -1, k, t * 10 + last - k)
94+
dfs(x * 10 + last - k)
9195

96+
ans = []
97+
boundary = 10 ** (n - 1)
9298
for i in range(1, 10):
93-
dfs(n -1, k, i)
99+
dfs(i)
94100
return ans
95101
```
96102

97103
#### Java
98104

99105
```java
100106
class Solution {
107+
private List<Integer> ans = new ArrayList<>();
108+
private int boundary;
109+
private int k;
110+
101111
public int[] numsSameConsecDiff(int n, int k) {
102-
List<Integer> res = new ArrayList<>();
112+
this.k = k;
113+
boundary = (int) Math.pow(10, n - 1);
103114
for (int i = 1; i < 10; ++i) {
104-
dfs(n -1, k, i, res);
115+
dfs(i);
105116
}
106-
int[] ans = new int[res.size()];
107-
for (int i = 0; i < res.size(); ++i) {
108-
ans[i] = res.get(i);
109-
}
110-
return ans;
117+
return ans.stream().mapToInt(i -> i).toArray();
111118
}
112119

113-
private void dfs(int n, intk, intt, List<Integer>res) {
114-
if (n ==0) {
115-
res.add(t);
120+
private void dfs(int x) {
121+
if (x >= boundary) {
122+
ans.add(x);
116123
return;
117124
}
118-
int last = t % 10;
119-
if (last + k <=9) {
120-
dfs(n -1, k, t * 10 + last + k, res);
125+
int last = x % 10;
126+
if (last + k <10) {
127+
dfs(x * 10 + last + k);
121128
}
122-
if (last - k >= 0 && k != 0) {
123-
dfs(n -1, k, t * 10 + last - k, res);
129+
if (k != 0 && last - k >= 0) {
130+
dfs(x * 10 + last - k);
124131
}
125132
}
126133
}
@@ -131,109 +138,111 @@ class Solution {
131138
```cpp
132139
class Solution {
133140
public:
134-
vector<int> ans;
135-
136141
vector<int> numsSameConsecDiff(int n, int k) {
137-
for (int i = 1; i < 10; ++i)
138-
dfs(n - 1, k, i);
139-
return ans;
140-
}
141-
142-
void dfs(int n, int k, int t) {
143-
if (n == 0) {
144-
ans.push_back(t);
145-
return;
142+
vector<int> ans;
143+
int boundary = pow(10, n - 1);
144+
auto dfs = [&](auto&& dfs, int x) {
145+
if (x >= boundary) {
146+
ans.push_back(x);
147+
return;
148+
}
149+
int last = x % 10;
150+
if (last + k < 10) {
151+
dfs(dfs, x * 10 + last + k);
152+
}
153+
if (k != 0 && last - k >= 0) {
154+
dfs(dfs, x * 10 + last - k);
155+
}
156+
};
157+
for (int i = 1; i < 10; ++i) {
158+
dfs(dfs, i);
146159
}
147-
int last = t % 10;
148-
if (last + k <= 9) dfs(n - 1, k, t * 10 + last + k);
149-
if (last - k >= 0 && k != 0) dfs(n - 1, k, t * 10 + last - k);
160+
return ans;
150161
}
151162
};
152163
```
153164
154165
#### Go
155166
156167
```go
157-
func numsSameConsecDiff(n int, k int) []int {
158-
var ans []int
159-
var dfs func(n, k, t int)
160-
dfs = func(n, k, t int) {
161-
if n == 0 {
162-
ans = append(ans, t)
168+
func numsSameConsecDiff(n int, k int) (ans []int) {
169+
bounary := int(math.Pow10(n - 1))
170+
var dfs func(int)
171+
dfs = func(x int) {
172+
if x >= bounary {
173+
ans = append(ans, x)
163174
return
164175
}
165-
last := t % 10
166-
if last+k <= 9 {
167-
dfs(n-1, k, t*10+last+k)
176+
last := x % 10
177+
if last+k < 10 {
178+
dfs(x*10 + last + k)
168179
}
169-
if last-k >= 0 && k != 0 {
170-
dfs(n-1, k, t*10+last-k)
180+
if k > 0 && last-k >= 0 {
181+
dfs(x*10 + last - k)
171182
}
172183
}
173-
174184
for i := 1; i < 10; i++ {
175-
dfs(n-1, k, i)
185+
dfs(i)
176186
}
177-
return ans
187+
return
178188
}
179189
```
180190

181191
#### TypeScript
182192

183193
```ts
184194
function numsSameConsecDiff(n: number, k: number): number[] {
185-
const ans=newSet<number>();
195+
const ans:number[] = [];
186196
const boundary = 10 ** (n - 1);
187-
188-
const dfs = (nums: number) => {
189-
if (nums >= boundary) {
190-
ans.add(nums);
197+
const dfs = (x: number) => {
198+
if (x >= boundary) {
199+
ans.push(x);
191200
return;
192201
}
193-
194-
const num =nums% 10;
195-
for (const x of [num + k, num-k]) {
196-
if (0<=x&&x<10) {
197-
dfs(nums*10+x);
198-
}
202+
const last =x%10;
203+
if (last+k< 10) {
204+
dfs(x*10 + last+k);
205+
}
206+
if (k>0&&last-k>=0) {
207+
dfs(x*10+last-k);
199208
}
200209
};
201-
202210
for (let i = 1; i < 10; i++) {
203211
dfs(i);
204212
}
205-
206-
return [...ans];
213+
return ans;
207214
}
208215
```
209216

210217
#### JavaScript
211218

212219
```js
213-
function numsSameConsecDiff(n, k) {
214-
const ans = new Set();
220+
/**
221+
* @param {number} n
222+
* @param {number} k
223+
* @return {number[]}
224+
*/
225+
var numsSameConsecDiff = function (n, k) {
226+
const ans = [];
215227
const boundary = 10 ** (n - 1);
216-
217-
const dfs = nums => {
218-
if (nums >= boundary) {
219-
ans.add(nums);
228+
const dfs = x => {
229+
if (x >= boundary) {
230+
ans.push(x);
220231
return;
221232
}
222-
223-
constnum= nums % 10;
224-
for (constxof [num + k, num - k]) {
225-
if (0<= x && x <10) {
226-
dfs(nums *10+ x);
227-
}
233+
constlast= x %10;
234+
if (last + k < 10) {
235+
dfs(x *10 + last + k);
236+
}
237+
if (k >0&& last - k >=0) {
238+
dfs(x *10+ last - k);
228239
}
229240
};
230-
231241
for (let i = 1; i < 10; i++) {
232242
dfs(i);
233243
}
234-
235-
return [...ans];
236-
}
244+
return ans;
245+
};
237246
```
238247

239248
<!-- tabs:end -->

0 commit comments

Comments
(0)

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