Skip to content

Commit c4ab91f

Browse files
committed
feat: add ts solution to lc problem: No.0137
1 parent c1a5db5 commit c4ab91f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,29 @@ function singleNumber(nums) {
402402

403403
<!-- solution:end -->
404404

405+
<!-- solution:start -->
406+
407+
### Solution 4: Bit Manipulation
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+
<!-- tabs:end -->
427+
428+
<!-- solution:end -->
429+
405430
<!-- problem:end -->

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,29 @@ function singleNumber(nums) {
393393

394394
<!-- solution:end -->
395395

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+
<!-- tabs:end -->
418+
419+
<!-- solution:end -->
420+
396421
<!-- problem:end -->
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)