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 ccf2322

Browse files
authored
feat: add solutions to lc problem: No.1823 (doocs#3220)
1 parent 424dc09 commit ccf2322

File tree

6 files changed

+156
-88
lines changed

6 files changed

+156
-88
lines changed

‎solution/1800-1899/1823.Find the Winner of the Circular Game/README.md‎

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -140,40 +140,71 @@ func findTheWinner(n int, k int) int {
140140
#### TypeScript
141141

142142
```ts
143-
class LinkNode {
144-
public val: number;
145-
public next: LinkNode;
146-
147-
constructor(val: number = 0, next?: LinkNode) {
148-
this.val = val;
149-
this.next = next;
143+
function findTheWinner(n: number, k: number): number {
144+
if (n === 1) {
145+
return 1;
150146
}
147+
const ans = (k + findTheWinner(n - 1, k)) % n;
148+
return ans ? ans : n;
151149
}
150+
```
152151

153-
function findTheWinner(n: number, k: number): number {
154-
if (k === 1) {
155-
return n;
152+
#### JavaScript
153+
154+
```js
155+
/**
156+
* @param {number} n
157+
* @param {number} k
158+
* @return {number}
159+
*/
160+
var findTheWinner = function (n, k) {
161+
if (n === 1) {
162+
return 1;
156163
}
157-
const dummy = new LinkNode(0);
158-
let cur = dummy;
159-
for (let i = 1; i <= n; i++) {
160-
cur.next = new LinkNode(i);
161-
cur = cur.next;
164+
const ans = (k + findTheWinner(n - 1, k)) % n;
165+
return ans ? ans : n;
166+
};
167+
```
168+
169+
<!-- tabs:end -->
170+
171+
<!-- solution:end -->
172+
173+
<!-- solution:start -->
174+
175+
### 方法二:模拟
176+
177+
<!-- tabs:start -->
178+
179+
#### TypeScript
180+
181+
```ts
182+
function findTheWinner(n: number, k: number): number {
183+
const arr = Array.from({ length: n }, (_, i) => i + 1);
184+
let i = 0;
185+
186+
while (arr.length > 1) {
187+
i = (i + k - 1) % arr.length;
188+
arr.splice(i, 1);
162189
}
163-
cur.next = dummy.next;
164-
165-
cur = dummy;
166-
let count = 0;
167-
while (cur.next != cur) {
168-
count++;
169-
if (count === k) {
170-
cur.next = cur.next.next;
171-
count = 0;
172-
} else {
173-
cur = cur.next;
174-
}
190+
191+
return arr[0];
192+
}
193+
```
194+
195+
#### JavaScript
196+
197+
```js
198+
function findTheWinner(n, k) {
199+
const arr = Array.from({ length: n }, (_, i) => i + 1);
200+
let i = 0;
201+
202+
while (arr.length > 1) {
203+
i = (i + k - 1) % arr.length;
204+
arr.splice(i, 1);
175205
}
176-
return cur.val;
206+
207+
return arr[0];
177208
}
178209
```
179210

‎solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md‎

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -139,40 +139,71 @@ func findTheWinner(n int, k int) int {
139139
#### TypeScript
140140

141141
```ts
142-
class LinkNode {
143-
public val: number;
144-
public next: LinkNode;
145-
146-
constructor(val: number = 0, next?: LinkNode) {
147-
this.val = val;
148-
this.next = next;
142+
function findTheWinner(n: number, k: number): number {
143+
if (n === 1) {
144+
return 1;
149145
}
146+
const ans = (k + findTheWinner(n - 1, k)) % n;
147+
return ans ? ans : n;
150148
}
149+
```
151150

152-
function findTheWinner(n: number, k: number): number {
153-
if (k === 1) {
154-
return n;
151+
#### JavaScript
152+
153+
```js
154+
/**
155+
* @param {number} n
156+
* @param {number} k
157+
* @return {number}
158+
*/
159+
var findTheWinner = function (n, k) {
160+
if (n === 1) {
161+
return 1;
155162
}
156-
const dummy = new LinkNode(0);
157-
let cur = dummy;
158-
for (let i = 1; i <= n; i++) {
159-
cur.next = new LinkNode(i);
160-
cur = cur.next;
163+
const ans = (k + findTheWinner(n - 1, k)) % n;
164+
return ans ? ans : n;
165+
};
166+
```
167+
168+
<!-- tabs:end -->
169+
170+
<!-- solution:end -->
171+
172+
<!-- solution:start -->
173+
174+
### Solution 2: Simulation
175+
176+
<!-- tabs:start -->
177+
178+
#### TypeScript
179+
180+
```ts
181+
function findTheWinner(n: number, k: number): number {
182+
const arr = Array.from({ length: n }, (_, i) => i + 1);
183+
let i = 0;
184+
185+
while (arr.length > 1) {
186+
i = (i + k - 1) % arr.length;
187+
arr.splice(i, 1);
161188
}
162-
cur.next = dummy.next;
163-
164-
cur = dummy;
165-
let count = 0;
166-
while (cur.next != cur) {
167-
count++;
168-
if (count === k) {
169-
cur.next = cur.next.next;
170-
count = 0;
171-
} else {
172-
cur = cur.next;
173-
}
189+
190+
return arr[0];
191+
}
192+
```
193+
194+
#### JavaScript
195+
196+
```js
197+
function findTheWinner(n, k) {
198+
const arr = Array.from({ length: n }, (_, i) => i + 1);
199+
let i = 0;
200+
201+
while (arr.length > 1) {
202+
i = (i + k - 1) % arr.length;
203+
arr.splice(i, 1);
174204
}
175-
return cur.val;
205+
206+
return arr[0];
176207
}
177208
```
178209

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var findTheWinner = function (n, k) {
7+
if (n === 1) {
8+
return 1;
9+
}
10+
const ans = (k + findTheWinner(n - 1, k)) % n;
11+
return ans ? ans : n;
12+
};
Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
class LinkNode {
2-
public val: number;
3-
public next: LinkNode;
4-
5-
constructor(val: number = 0, next?: LinkNode) {
6-
this.val = val;
7-
this.next = next;
8-
}
9-
}
10-
111
function findTheWinner(n: number, k: number): number {
12-
if (k === 1) {
13-
return n;
14-
}
15-
const dummy = new LinkNode(0);
16-
let cur = dummy;
17-
for (let i = 1; i <= n; i++) {
18-
cur.next = new LinkNode(i);
19-
cur = cur.next;
20-
}
21-
cur.next = dummy.next;
22-
23-
cur = dummy;
24-
let count = 0;
25-
while (cur.next != cur) {
26-
count++;
27-
if (count === k) {
28-
cur.next = cur.next.next;
29-
count = 0;
30-
} else {
31-
cur = cur.next;
32-
}
2+
if (n === 1) {
3+
return 1;
334
}
34-
return cur.val;
5+
const ans = (k + findTheWinner(n - 1, k)) % n;
6+
return ans ? ans : n;
357
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function findTheWinner(n, k) {
2+
const arr = Array.from({ length: n }, (_, i) => i + 1);
3+
let i = 0;
4+
5+
while (arr.length > 1) {
6+
i = (i + k - 1) % arr.length;
7+
arr.splice(i, 1);
8+
}
9+
10+
return arr[0];
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function findTheWinner(n: number, k: number): number {
2+
const arr = Array.from({ length: n }, (_, i) => i + 1);
3+
let i = 0;
4+
5+
while (arr.length > 1) {
6+
i = (i + k - 1) % arr.length;
7+
arr.splice(i, 1);
8+
}
9+
10+
return arr[0];
11+
}

0 commit comments

Comments
(0)

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