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 2dcd8ce

Browse files
committed
feat: add solutions to lc problems
- No.0017.Letter Combinations of a Phone Number - No.0022.Generate Parentheses - No.0079.Word Search - No.0633.Sum of Square Numbers
1 parent d7db53e commit 2dcd8ce

File tree

13 files changed

+516
-79
lines changed

13 files changed

+516
-79
lines changed

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ impl Solution {
305305
}
306306
```
307307

308+
```rust
309+
impl Solution {
310+
fn dfs(i: usize, digits: &[u8], map: &Vec<Vec<char>>, s: &mut String, res: &mut Vec<String>) {
311+
if i == digits.len() {
312+
res.push(s.clone());
313+
return;
314+
}
315+
for c in map[(digits[i] - b'2') as usize].iter() {
316+
s.push(*c);
317+
Self::dfs(i + 1, digits, map, s, res);
318+
s.pop();
319+
}
320+
}
321+
322+
pub fn letter_combinations(digits: String) -> Vec<String> {
323+
if digits.is_empty() {
324+
return Vec::new();
325+
}
326+
let digits = digits.as_bytes();
327+
let map = vec![
328+
vec!['a', 'b', 'c'],
329+
vec!['d', 'e', 'f'],
330+
vec!['g', 'h', 'i'],
331+
vec!['j', 'k', 'l'],
332+
vec!['m', 'n', 'o'],
333+
vec!['p', 'q', 'r', 's'],
334+
vec!['t', 'u', 'v'],
335+
vec!['w', 'x', 'y', 'z'],
336+
];
337+
let mut res = Vec::new();
338+
Self::dfs(0, digits, &map, &mut String::new(), &mut res);
339+
res
340+
}
341+
}
342+
```
343+
308344
### **...**
309345

310346
```

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,42 @@ impl Solution {
293293
}
294294
```
295295

296+
```rust
297+
impl Solution {
298+
fn dfs(i: usize, digits: &[u8], map: &Vec<Vec<char>>, s: &mut String, res: &mut Vec<String>) {
299+
if i == digits.len() {
300+
res.push(s.clone());
301+
return;
302+
}
303+
for c in map[(digits[i] - b'2') as usize].iter() {
304+
s.push(*c);
305+
Self::dfs(i + 1, digits, map, s, res);
306+
s.pop();
307+
}
308+
}
309+
310+
pub fn letter_combinations(digits: String) -> Vec<String> {
311+
if digits.is_empty() {
312+
return Vec::new();
313+
}
314+
let digits = digits.as_bytes();
315+
let map = vec![
316+
vec!['a', 'b', 'c'],
317+
vec!['d', 'e', 'f'],
318+
vec!['g', 'h', 'i'],
319+
vec!['j', 'k', 'l'],
320+
vec!['m', 'n', 'o'],
321+
vec!['p', 'q', 'r', 's'],
322+
vec!['t', 'u', 'v'],
323+
vec!['w', 'x', 'y', 'z'],
324+
];
325+
let mut res = Vec::new();
326+
Self::dfs(0, digits, &map, &mut String::new(), &mut res);
327+
res
328+
}
329+
}
330+
```
331+
296332
### **...**
297333

298334
```
Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,33 @@
1-
use std::collections::HashMap;
2-
31
impl Solution {
4-
fn dfs(
5-
i: usize,
6-
s: &mut String,
7-
cs: &Vec<char>,
8-
map: &HashMap<char, String>,
9-
res: &mut Vec<String>,
10-
) {
11-
if i == cs.len() {
2+
fn dfs(i: usize, digits: &[u8], map: &Vec<Vec<char>>, s: &mut String, res: &mut Vec<String>) {
3+
if i == digits.len() {
124
res.push(s.clone());
135
return;
146
}
15-
for c in map.get(&cs[i]).unwrap().chars() {
16-
s.push(c);
17-
Self::dfs(i + 1, s, cs, map, res);
7+
for c in map[(digits[i] - b'2')asusize].iter() {
8+
s.push(*c);
9+
Self::dfs(i + 1, digits, map, s, res);
1810
s.pop();
1911
}
2012
}
2113

2214
pub fn letter_combinations(digits: String) -> Vec<String> {
23-
let mut res = vec![];
2415
if digits.is_empty() {
25-
return res;
16+
return Vec::new();
2617
}
27-
28-
let mut map = HashMap::new();
29-
map.insert('2', String::from("abc"));
30-
map.insert('3', String::from("def"));
31-
map.insert('4', String::from("ghi"));
32-
map.insert('5', String::from("jkl"));
33-
map.insert('6', String::from("mno"));
34-
map.insert('7', String::from("pqrs"));
35-
map.insert('8', String::from("tuv"));
36-
map.insert('9', String::from("wxyz"));
37-
38-
Self::dfs(
39-
0,
40-
&mut String::new(),
41-
&digits.chars().collect(),
42-
&map,
43-
&mut res,
44-
);
18+
let digits = digits.as_bytes();
19+
let map = vec![
20+
vec!['a', 'b', 'c'],
21+
vec!['d', 'e', 'f'],
22+
vec!['g', 'h', 'i'],
23+
vec!['j', 'k', 'l'],
24+
vec!['m', 'n', 'o'],
25+
vec!['p', 'q', 'r', 's'],
26+
vec!['t', 'u', 'v'],
27+
vec!['w', 'x', 'y', 'z'],
28+
];
29+
let mut res = Vec::new();
30+
Self::dfs(0, digits, &map, &mut String::new(), &mut res);
4531
res
4632
}
4733
}

‎solution/0000-0099/0022.Generate Parentheses/README.md‎

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,6 @@ class Solution {
8888
}
8989
```
9090

91-
### **TypeScript**
92-
93-
```ts
94-
function generateParenthesis(n: number): string[] {
95-
let ans = [];
96-
let dfs = function (left, right, t) {
97-
if (left == n && right == n) {
98-
ans.push(t);
99-
return;
100-
}
101-
if (left < n) {
102-
dfs(left + 1, right, t + '(');
103-
}
104-
if (right < left) {
105-
dfs(left, right + 1, t + ')');
106-
}
107-
};
108-
dfs(0, 0, '');
109-
return ans;
110-
}
111-
```
112-
11391
### **C++**
11492

11593
```cpp
@@ -182,6 +160,57 @@ var generateParenthesis = function (n) {
182160
};
183161
```
184162

163+
### **TypeScript**
164+
165+
```ts
166+
function generateParenthesis(n: number): string[] {
167+
const ans: string[] = [];
168+
const dfs = (left: number, right: number, t: string) => {
169+
if (left == n && right == n) {
170+
ans.push(t);
171+
return;
172+
}
173+
if (left < n) {
174+
dfs(left + 1, right, t + '(');
175+
}
176+
if (right < left) {
177+
dfs(left, right + 1, t + ')');
178+
}
179+
};
180+
dfs(0, 0, '');
181+
return ans;
182+
}
183+
```
184+
185+
### **Rust**
186+
187+
```rust
188+
impl Solution {
189+
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
190+
if left == 0 && right == 0 {
191+
res.push(s.clone());
192+
return;
193+
}
194+
if left > 0 {
195+
s.push('(');
196+
Self::dfs(left - 1, right, s, res);
197+
s.pop();
198+
}
199+
if right > left {
200+
s.push(')');
201+
Self::dfs(left, right - 1, s, res);
202+
s.pop();
203+
}
204+
}
205+
206+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
207+
let mut res = Vec::new();
208+
Self::dfs(n, n, &mut String::new(), &mut res);
209+
res
210+
}
211+
}
212+
```
213+
185214
### **...**
186215

187216
```

‎solution/0000-0099/0022.Generate Parentheses/README_EN.md‎

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,6 @@ class Solution {
7171
}
7272
```
7373

74-
### **TypeScript**
75-
76-
```ts
77-
function generateParenthesis(n: number): string[] {
78-
let ans = [];
79-
let dfs = function (left, right, t) {
80-
if (left == n && right == n) {
81-
ans.push(t);
82-
return;
83-
}
84-
if (left < n) {
85-
dfs(left + 1, right, t + '(');
86-
}
87-
if (right < left) {
88-
dfs(left, right + 1, t + ')');
89-
}
90-
};
91-
dfs(0, 0, '');
92-
return ans;
93-
}
94-
```
95-
9674
### **C++**
9775

9876
```cpp
@@ -165,6 +143,57 @@ var generateParenthesis = function (n) {
165143
};
166144
```
167145

146+
### **TypeScript**
147+
148+
```ts
149+
function generateParenthesis(n: number): string[] {
150+
const ans: string[] = [];
151+
const dfs = (left: number, right: number, t: string) => {
152+
if (left == n && right == n) {
153+
ans.push(t);
154+
return;
155+
}
156+
if (left < n) {
157+
dfs(left + 1, right, t + '(');
158+
}
159+
if (right < left) {
160+
dfs(left, right + 1, t + ')');
161+
}
162+
};
163+
dfs(0, 0, '');
164+
return ans;
165+
}
166+
```
167+
168+
### **Rust**
169+
170+
```rust
171+
impl Solution {
172+
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
173+
if left == 0 && right == 0 {
174+
res.push(s.clone());
175+
return;
176+
}
177+
if left > 0 {
178+
s.push('(');
179+
Self::dfs(left - 1, right, s, res);
180+
s.pop();
181+
}
182+
if right > left {
183+
s.push(')');
184+
Self::dfs(left, right - 1, s, res);
185+
s.pop();
186+
}
187+
}
188+
189+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
190+
let mut res = Vec::new();
191+
Self::dfs(n, n, &mut String::new(), &mut res);
192+
res
193+
}
194+
}
195+
```
196+
168197
### **...**
169198

170199
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
3+
if left == 0 && right == 0 {
4+
res.push(s.clone());
5+
return;
6+
}
7+
if left > 0 {
8+
s.push('(');
9+
Self::dfs(left - 1, right, s, res);
10+
s.pop();
11+
}
12+
if right > left {
13+
s.push(')');
14+
Self::dfs(left, right - 1, s, res);
15+
s.pop();
16+
}
17+
}
18+
19+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
20+
let mut res = Vec::new();
21+
Self::dfs(n, n, &mut String::new(), &mut res);
22+
res
23+
}
24+
}

‎solution/0000-0099/0022.Generate Parentheses/Solution.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function generateParenthesis(n: number): string[] {
2-
let ans = [];
3-
let dfs = function(left, right, t) {
2+
const ans: string[] = [];
3+
const dfs = (left: number, right: number, t: string)=> {
44
if (left == n && right == n) {
55
ans.push(t);
66
return;

0 commit comments

Comments
(0)

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