diff --git a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README.md b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README.md index 96abb63003f46..41251e6250970 100644 --- a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README.md +++ b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README.md @@ -178,6 +178,64 @@ func numsSameConsecDiff(n int, k int) []int { } ``` +#### TypeScript + +```ts +function numsSameConsecDiff(n: number, k: number): number[] { + const ans = new Set(); + const boundary = 10 ** (n - 1); + + const dfs = (nums: number) => { + if (nums >= boundary) { + ans.add(nums); + return; + } + + const num = nums % 10; + for (const x of [num + k, num - k]) { + if (0 <= x && x < 10) { + dfs(nums * 10 + x); + } + } + }; + + for (let i = 1; i < 10; i++) { + dfs(i); + } + + return [...ans]; +} +``` + +#### JavaScript + +```js +function numsSameConsecDiff(n, k) { + const ans = new Set(); + const boundary = 10 ** (n - 1); + + const dfs = nums => { + if (nums >= boundary) { + ans.add(nums); + return; + } + + const num = nums % 10; + for (const x of [num + k, num - k]) { + if (0 <= x && x < 10) { + dfs(nums * 10 + x); + } + } + }; + + for (let i = 1; i < 10; i++) { + dfs(i); + } + + return [...ans]; +} +``` + diff --git a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README_EN.md b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README_EN.md index fa6a8d6bf0c8e..07361120c47c5 100644 --- a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README_EN.md +++ b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/README_EN.md @@ -161,6 +161,64 @@ func numsSameConsecDiff(n int, k int) []int { } ``` +#### TypeScript + +```ts +function numsSameConsecDiff(n: number, k: number): number[] { + const ans = new Set(); + const boundary = 10 ** (n - 1); + + const dfs = (nums: number) => { + if (nums >= boundary) { + ans.add(nums); + return; + } + + const num = nums % 10; + for (const x of [num + k, num - k]) { + if (0 <= x && x < 10) { + dfs(nums * 10 + x); + } + } + }; + + for (let i = 1; i < 10; i++) { + dfs(i); + } + + return [...ans]; +} +``` + +#### JavaScript + +```js +function numsSameConsecDiff(n, k) { + const ans = new Set(); + const boundary = 10 ** (n - 1); + + const dfs = nums => { + if (nums >= boundary) { + ans.add(nums); + return; + } + + const num = nums % 10; + for (const x of [num + k, num - k]) { + if (0 <= x && x < 10) { + dfs(nums * 10 + x); + } + } + }; + + for (let i = 1; i < 10; i++) { + dfs(i); + } + + return [...ans]; +} +``` + diff --git a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.js b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.js new file mode 100644 index 0000000000000..29fe44aa4c446 --- /dev/null +++ b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.js @@ -0,0 +1,24 @@ +function numsSameConsecDiff(n, k) { + const ans = new Set(); + const boundary = 10 ** (n - 1); + + const dfs = nums => { + if (nums >= boundary) { + ans.add(nums); + return; + } + + const num = nums % 10; + for (const x of [num + k, num - k]) { + if (0 <= x && x < 10) { + dfs(nums * 10 + x); + } + } + }; + + for (let i = 1; i < 10; i++) { + dfs(i); + } + + return [...ans]; +} diff --git a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.ts b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.ts new file mode 100644 index 0000000000000..8da7c4da8a91c --- /dev/null +++ b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.ts @@ -0,0 +1,24 @@ +function numsSameConsecDiff(n: number, k: number): number[] { + const ans = new Set(); + const boundary = 10 ** (n - 1); + + const dfs = (nums: number) => { + if (nums >= boundary) { + ans.add(nums); + return; + } + + const num = nums % 10; + for (const x of [num + k, num - k]) { + if (0 <= x && x < 10) { + dfs(nums * 10 + x); + } + } + }; + + for (let i = 1; i < 10; i++) { + dfs(i); + } + + return [...ans]; +}