LeetCode - Algorithms - 137. Single Number II

Problem

137. Single Number II

Java

Bitwise

We can design the digital logic what we need so the only one can still be extracted.

© https://gist.githubusercontent.com/lenchen1112/77dfd4afae0098dbe3199e4b70fa6a2b/raw/ceb58d8d505ddab95b133eb527235eaa77be2a1a/137_single_number_II.py

1
2
3
4
5
6
7
8
9
10
class Solution {
public int singleNumber(int[] nums) {
int lowBits = 0, highBits = 0;
for (int num : nums) {
lowBits = ~highBits & (lowBits ^ num);
highBits = ~lowBits & (highBits ^ num);
}
return lowBits;
}
}

Submission Detail

  • 14 / 14 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Single Number II.
  • Memory Usage: 38.8 MB, less than 49.17% of Java online submissions for Single Number II.