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

feat: update js solution to lc problem: No.0017 #3216

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

Closed
rain84 wants to merge 2 commits into doocs:main from rain84:feature/lc-0017
Closed
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
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -392,25 +392,27 @@ func letterCombinations(digits string) (ans []string) {

```ts
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [];
const t: string[] = [];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
const dfs = (i: number) => {
if (i >= digits.length) {
ans.push(t.join(''));
const n = digits.length;
const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [
...x,
]);

const dfs = (curr: string, start: number) => {
if (curr.length === n) {
ans.push(curr);
return;
}
const s = d[parseInt(digits[i]) - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
t.pop();
for (let i = start; i < n; i++) {
for (const ch of d[+digits[i] - 2]) {
dfs(curr + ch, i + 1);
}
}
};
dfs(0);
dfs('', 0);
return ans;
}
```
Expand Down Expand Up @@ -453,25 +455,25 @@ impl Solution {
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [];
const t = [];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
const dfs = i => {
if (i >= digits.length) {
ans.push(t.join(''));
const n = digits.length;
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]);

const dfs = (curr, start) => {
if (curr.length === n) {
ans.push(curr);
return;
}
const s = d[parseInt(digits[i]) - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
t.pop();
for (let i = start; i < n; i++) {
for (const ch of map[+digits[i] - 2]) {
dfs(curr + ch, i + 1);
}
}
};
dfs(0);
dfs('', 0);
return ans;
};
```
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -388,25 +388,27 @@ func letterCombinations(digits string) (ans []string) {

```ts
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [];
const t: string[] = [];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
const dfs = (i: number) => {
if (i >= digits.length) {
ans.push(t.join(''));
const n = digits.length;
const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [
...x,
]);

const dfs = (curr: string, start: number) => {
if (curr.length === n) {
ans.push(curr);
return;
}
const s = d[parseInt(digits[i]) - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
t.pop();
for (let i = start; i < n; i++) {
for (const ch of d[+digits[i] - 2]) {
dfs(curr + ch, i + 1);
}
}
};
dfs(0);
dfs('', 0);
return ans;
}
```
Expand Down Expand Up @@ -449,25 +451,25 @@ impl Solution {
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [];
const t = [];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
const dfs = i => {
if (i >= digits.length) {
ans.push(t.join(''));
const n = digits.length;
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]);

const dfs = (curr, start) => {
if (curr.length === n) {
ans.push(curr);
return;
}
const s = d[parseInt(digits[i]) - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
t.pop();
for (let i = start; i < n; i++) {
for (const ch of map[+digits[i] - 2]) {
dfs(curr + ch, i + 1);
}
}
};
dfs(0);
dfs('', 0);
return ans;
};
```
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [];
const t = [];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
const dfs = i => {
if (i >= digits.length) {
ans.push(t.join(''));
const n = digits.length;
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]);

const dfs = (curr, start) => {
if (curr.length === n) {
ans.push(curr);
return;
}
const s = d[parseInt(digits[i]) - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
t.pop();
for (let i = start; i < n; i++) {
for (const ch of map[+digits[i] - 2]) {
dfs(curr + ch, i + 1);
}
}
};
dfs(0);
dfs('', 0);
return ans;
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [];
const t: string[] = [];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
const dfs = (i: number) => {
if (i >= digits.length) {
ans.push(t.join(''));
const n = digits.length;
const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [
...x,
]);

const dfs = (curr: string, start: number) => {
if (curr.length === n) {
ans.push(curr);
return;
}
const s = d[parseInt(digits[i]) - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
t.pop();
for (let i = start; i < n; i++) {
for (const ch of d[+digits[i] - 2]) {
dfs(curr + ch, i + 1);
}
}
};
dfs(0);
dfs('', 0);
return ans;
}

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