Skip to content

Commit c02bbdb

Browse files
committedJan 7, 2023
Add solution #137
1 parent 563575e commit c02bbdb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
134|[Gas Station](./0134-gas-station.js)|Medium|
8383
135|[Candy](./0135-candy.js)|Hard|
8484
136|[Single Number](./0136-single-number.js)|Easy|
85+
137|[Single Number II](./0137-single-number-ii.js)|Medium|
8586
141|[Linked List Cycle](./0141-linked-list-cycle.js)|Easy|
8687
142|[Linked List Cycle II](./0142-linked-list-cycle-ii.js)|Medium|
8788
143|[Reorder List](./0143-reorder-list.js)|Medium|

‎solutions/0137-single-number-ii.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 137. Single Number II
3+
* https://leetcode.com/problems/single-number-ii/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums where every element appears three times except for one, which appears
7+
* exactly once. Find the single element and return it.
8+
*
9+
* You must implement a solution with a linear runtime complexity and use only constant extra space.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @return {number}
15+
*/
16+
var singleNumber = function(nums) {
17+
let a = 0, b = 0;
18+
19+
nums.forEach(c => {
20+
const offset = (~a & b & c) | (a & ~b & ~c);
21+
b = (~a & ~b & c) | (~a & b & ~c);
22+
a = offset;
23+
});
24+
25+
return a | b;
26+
};

0 commit comments

Comments
 (0)