diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md index 185dc5936224e..2233b825aaa05 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md @@ -140,40 +140,71 @@ func findTheWinner(n int, k int) int { #### TypeScript ```ts -class LinkNode { - public val: number; - public next: LinkNode; - - constructor(val: number = 0, next?: LinkNode) { - this.val = val; - this.next = next; +function findTheWinner(n: number, k: number): number { + if (n === 1) { + return 1; } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; } +``` -function findTheWinner(n: number, k: number): number { - if (k === 1) { - return n; +#### JavaScript + +```js +/** + * @param {number} n + * @param {number} k + * @return {number} + */ +var findTheWinner = function (n, k) { + if (n === 1) { + return 1; } - const dummy = new LinkNode(0); - let cur = dummy; - for (let i = 1; i <= n; i++) { - cur.next = new LinkNode(i); - cur = cur.next; + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; +}; +``` + + + + + + + +### 方法二:模拟 + + + +#### TypeScript + +```ts +function findTheWinner(n: number, k: number): number { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); } - cur.next = dummy.next; - - cur = dummy; - let count = 0; - while (cur.next != cur) { - count++; - if (count === k) { - cur.next = cur.next.next; - count = 0; - } else { - cur = cur.next; - } + + return arr[0]; +} +``` + +#### JavaScript + +```js +function findTheWinner(n, k) { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); } - return cur.val; + + return arr[0]; } ``` diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md index 800d1682c5d8f..ae70a45fec097 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md @@ -139,40 +139,71 @@ func findTheWinner(n int, k int) int { #### TypeScript ```ts -class LinkNode { - public val: number; - public next: LinkNode; - - constructor(val: number = 0, next?: LinkNode) { - this.val = val; - this.next = next; +function findTheWinner(n: number, k: number): number { + if (n === 1) { + return 1; } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; } +``` -function findTheWinner(n: number, k: number): number { - if (k === 1) { - return n; +#### JavaScript + +```js +/** + * @param {number} n + * @param {number} k + * @return {number} + */ +var findTheWinner = function (n, k) { + if (n === 1) { + return 1; } - const dummy = new LinkNode(0); - let cur = dummy; - for (let i = 1; i <= n; i++) { - cur.next = new LinkNode(i); - cur = cur.next; + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; +}; +``` + + + + + + + +### Solution 2: Simulation + + + +#### TypeScript + +```ts +function findTheWinner(n: number, k: number): number { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); } - cur.next = dummy.next; - - cur = dummy; - let count = 0; - while (cur.next != cur) { - count++; - if (count === k) { - cur.next = cur.next.next; - count = 0; - } else { - cur = cur.next; - } + + return arr[0]; +} +``` + +#### JavaScript + +```js +function findTheWinner(n, k) { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); } - return cur.val; + + return arr[0]; } ``` diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js new file mode 100644 index 0000000000000..c0be3fb81487b --- /dev/null +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js @@ -0,0 +1,12 @@ +/** + * @param {number} n + * @param {number} k + * @return {number} + */ +var findTheWinner = function (n, k) { + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; +}; diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts index 047f95c64859c..6cf955dca4825 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts @@ -1,35 +1,7 @@ -class LinkNode { - public val: number; - public next: LinkNode; - - constructor(val: number = 0, next?: LinkNode) { - this.val = val; - this.next = next; - } -} - function findTheWinner(n: number, k: number): number { - if (k === 1) { - return n; - } - const dummy = new LinkNode(0); - let cur = dummy; - for (let i = 1; i <= n; i++) { - cur.next = new LinkNode(i); - cur = cur.next; - } - cur.next = dummy.next; - - cur = dummy; - let count = 0; - while (cur.next != cur) { - count++; - if (count === k) { - cur.next = cur.next.next; - count = 0; - } else { - cur = cur.next; - } + if (n === 1) { + return 1; } - return cur.val; + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; } diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js new file mode 100644 index 0000000000000..77b3d801c9aba --- /dev/null +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js @@ -0,0 +1,11 @@ +function findTheWinner(n, k) { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts new file mode 100644 index 0000000000000..a94ffa41d294b --- /dev/null +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts @@ -0,0 +1,11 @@ +function findTheWinner(n: number, k: number): number { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +}