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 9bb5f0e

Browse files
authored
feat: add js/ts solutions to lc problem: No.0137 (#3374)
1 parent d526620 commit 9bb5f0e

File tree

8 files changed

+247
-0
lines changed

8 files changed

+247
-0
lines changed

‎solution/0100-0199/0137.Single Number II/README.md‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ function singleNumber(nums: number[]): number {
146146
}
147147
```
148148

149+
#### JavaScript
150+
151+
```js
152+
function singleNumber(nums) {
153+
let ans = 0;
154+
for (let i = 0; i < 32; i++) {
155+
const count = nums.reduce((r, v) => r + ((v >> i) & 1), 0);
156+
ans |= count % 3 << i;
157+
}
158+
return ans;
159+
}
160+
```
161+
149162
#### Rust
150163

151164
```rust
@@ -319,6 +332,22 @@ function singleNumber(nums: number[]): number {
319332
}
320333
```
321334

335+
#### JavaScript
336+
337+
```js
338+
function singleNumber(nums) {
339+
let a = 0;
340+
let b = 0;
341+
for (const c of nums) {
342+
const aa = (~a & b & c) | (a & ~b & ~c);
343+
const bb = ~a & (b ^ c);
344+
a = aa;
345+
b = bb;
346+
}
347+
return b;
348+
}
349+
```
350+
322351
#### Rust
323352

324353
```rust
@@ -343,4 +372,74 @@ impl Solution {
343372

344373
<!-- solution:end -->
345374

375+
<!-- solution:start -->
376+
377+
### 方法三:哈希表 + 数学
378+
379+
<!-- tabs:start -->
380+
381+
#### TypeScript
382+
383+
```ts
384+
function singleNumber(nums: number[]): number {
385+
const sumOfUnique = [...new Set(nums)].reduce((a, b) => a + b, 0);
386+
const sum = nums.reduce((a, b) => a + b, 0);
387+
return (sumOfUnique * 3 - sum) / 2;
388+
}
389+
```
390+
391+
#### JavaScript
392+
393+
```js
394+
function singleNumber(nums) {
395+
const sumOfUnique = [...new Set(nums)].reduce((a, b) => a + b, 0);
396+
const sum = nums.reduce((a, b) => a + b, 0);
397+
return (sumOfUnique * 3 - sum) / 2;
398+
}
399+
```
400+
401+
<!-- tabs:end -->
402+
403+
<!-- solution:end -->
404+
405+
<!-- solution:start -->
406+
407+
### 方法四:位运算
408+
409+
<!-- tabs:start -->
410+
411+
#### TypeScript
412+
413+
```ts
414+
function singleNumber(nums: number[]): number {
415+
let [ans, acc] = [0, 0];
416+
417+
for (const x of nums) {
418+
ans ^= x & ~acc;
419+
acc ^= x & ~ans;
420+
}
421+
422+
return ans;
423+
}
424+
```
425+
426+
#### JavaScript
427+
428+
```ts
429+
function singleNumber(nums) {
430+
let [ans, acc] = [0, 0];
431+
432+
for (const x of nums) {
433+
ans ^= x & ~acc;
434+
acc ^= x & ~ans;
435+
}
436+
437+
return ans;
438+
}
439+
```
440+
441+
<!-- tabs:end -->
442+
443+
<!-- solution:end -->
444+
346445
<!-- problem:end -->

‎solution/0100-0199/0137.Single Number II/README_EN.md‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ function singleNumber(nums: number[]): number {
137137
}
138138
```
139139

140+
#### JavaScript
141+
142+
```js
143+
function singleNumber(nums) {
144+
let ans = 0;
145+
for (let i = 0; i < 32; i++) {
146+
const count = nums.reduce((r, v) => r + ((v >> i) & 1), 0);
147+
ans |= count % 3 << i;
148+
}
149+
return ans;
150+
}
151+
```
152+
140153
#### Rust
141154

142155
```rust
@@ -310,6 +323,22 @@ function singleNumber(nums: number[]): number {
310323
}
311324
```
312325

326+
#### JavaScript
327+
328+
```js
329+
function singleNumber(nums) {
330+
let a = 0;
331+
let b = 0;
332+
for (const c of nums) {
333+
const aa = (~a & b & c) | (a & ~b & ~c);
334+
const bb = ~a & (b ^ c);
335+
a = aa;
336+
b = bb;
337+
}
338+
return b;
339+
}
340+
```
341+
313342
#### Rust
314343

315344
```rust
@@ -334,4 +363,74 @@ impl Solution {
334363

335364
<!-- solution:end -->
336365

366+
<!-- solution:start -->
367+
368+
### Solution 3: Set + Math
369+
370+
<!-- tabs:start -->
371+
372+
#### TypeScript
373+
374+
```ts
375+
function singleNumber(nums: number[]): number {
376+
const sumOfUnique = [...new Set(nums)].reduce((a, b) => a + b, 0);
377+
const sum = nums.reduce((a, b) => a + b, 0);
378+
return (sumOfUnique * 3 - sum) / 2;
379+
}
380+
```
381+
382+
#### JavaScript
383+
384+
```js
385+
function singleNumber(nums) {
386+
const sumOfUnique = [...new Set(nums)].reduce((a, b) => a + b, 0);
387+
const sum = nums.reduce((a, b) => a + b, 0);
388+
return (sumOfUnique * 3 - sum) / 2;
389+
}
390+
```
391+
392+
<!-- tabs:end -->
393+
394+
<!-- solution:end -->
395+
396+
<!-- solution:start -->
397+
398+
### Solution 4: Bit Manipulation
399+
400+
<!-- tabs:start -->
401+
402+
#### TypeScript
403+
404+
```ts
405+
function singleNumber(nums: number[]): number {
406+
let [ans, acc] = [0, 0];
407+
408+
for (const x of nums) {
409+
ans ^= x & ~acc;
410+
acc ^= x & ~ans;
411+
}
412+
413+
return ans;
414+
}
415+
```
416+
417+
#### JavaScript
418+
419+
```ts
420+
function singleNumber(nums) {
421+
let [ans, acc] = [0, 0];
422+
423+
for (const x of nums) {
424+
ans ^= x & ~acc;
425+
acc ^= x & ~ans;
426+
}
427+
428+
return ans;
429+
}
430+
```
431+
432+
<!-- tabs:end -->
433+
434+
<!-- solution:end -->
435+
337436
<!-- problem:end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function singleNumber(nums) {
2+
let ans = 0;
3+
for (let i = 0; i < 32; i++) {
4+
const count = nums.reduce((r, v) => r + ((v >> i) & 1), 0);
5+
ans |= count % 3 << i;
6+
}
7+
return ans;
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function singleNumber(nums) {
2+
let a = 0;
3+
let b = 0;
4+
for (const c of nums) {
5+
const aa = (~a & b & c) | (a & ~b & ~c);
6+
const bb = ~a & (b ^ c);
7+
a = aa;
8+
b = bb;
9+
}
10+
return b;
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function singleNumber(nums) {
2+
const sumOfUnique = [...new Set(nums)].reduce((a, b) => a + b, 0);
3+
const sum = nums.reduce((a, b) => a + b, 0);
4+
return (sumOfUnique * 3 - sum) / 2;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function singleNumber(nums: number[]): number {
2+
const sumOfUnique = [...new Set(nums)].reduce((a, b) => a + b, 0);
3+
const sum = nums.reduce((a, b) => a + b, 0);
4+
return (sumOfUnique * 3 - sum) / 2;
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function singleNumber(nums) {
2+
let [ans, acc] = [0, 0];
3+
4+
for (const x of nums) {
5+
ans ^= x & ~acc;
6+
acc ^= x & ~ans;
7+
}
8+
9+
return ans;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function singleNumber(nums: number[]): number {
2+
let [ans, acc] = [0, 0];
3+
4+
for (const x of nums) {
5+
ans ^= x & ~acc;
6+
acc ^= x & ~ans;
7+
}
8+
9+
return ans;
10+
}

0 commit comments

Comments
(0)

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