Skip to content

feat: add js solution to lc problem: No.1823 #3220

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

Merged
merged 8 commits into from
Jul 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:模拟

<!-- tabs:start -->

#### 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];
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Simulation

<!-- tabs:start -->

#### 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];
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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];
}
Original file line number Diff line number Diff line change
@@ -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];
}
Loading