https://leetcode.com/problems/single-number/
Given an array of integers, every element appears twice except for one. Find that single one.Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Single Number I
Solution 1: With HashSet
Time Complexity: O(N^2), Space Complexity: O(N);
1 | public class Solution { |
Solution 2: With Sort Algorithm
Time Complexity: O(NlogN), Space Complexity: O(1);
1 | public class Solution { |
Solution 3: With XOR Operation
reference: https://leetcode.com/discuss/94594/java-solution-time-complexity-and-space-complexity-using-xor
refernce: https://leetcode.com/discuss/85396/how-can-xor-keep-many-numbers
Time Complexity: O(N), Space Complexity: O(1);
The idea hinges on 3 properties of xor. (1) that its a commutative operation (i.e. a xor b = b xor a). (2) that something xor itself is 0. So a xor a = 0. And (3) 0 xor a = a. These three properties mean that
a xor b xor a = a xor a xor b = 0 xor b = b.
Thus it doesn’t matter the order of the numbers. If something only occurs once it won’t get negated.
1 | public class Solution { |
Single Number II
https://leetcode.com/problems/single-number-ii/
Given an array of integers, every element appears three times except for one. Find that single one.Note:
Your algorithm should have a linear runtime complexity.
Solution 1: With HashMap
1 | public class Solution { |
Solution 2: With Bit Manipulation
For a 32-bit integer, if this integer appears three times in the input array, each digit of this integer will apppears three times. While if it only appears once in the input array, each digit of this integer will appeaars only once.
1 | public class Solution { |
Single Number III
https://leetcode.com/problems/single-number-iii/
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:
Given nums =
[1, 2, 1, 3, 2, 5], return[3, 5].Note:
- The order of the result is not important. So in the above example,
[5, 3]is also correct.- Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
Solution 1: With HashMap
https://leetcode.com/discuss/95580/java-easy-to-understand-solution-with-map
1 | public class Solution { |
Solution 2: With XOR Operation
Use XOR for all Integer in the input array, the result is the XOR result between two number like result = 3^5. We know that 3 is not equal to 5 so that 3^5 is not eqaul to 0 and there must be one digit in 3 and 5 are not equal, for example, this digit in 3 is 0 while is 1 in 5. (if not, 3^5 will be 0)
With this property, we can divide the input array inot two groups: one group contains all integer with 0 in that digit, and the another contain all integer with 1 in that digit.
Use XOR for these two groups separately, we will get 3 and 5 separately. And then with 3^result = 5 and 5^result = 3 we will find the answer of this question.
1 | public class Solution { |
More Reference:
http://blog.sina.com.cn/s/blog_77dd9d490102vwew.html
http://blog.csdn.net/lcj_cjfykx/article/details/48531073
http://believerw.github.io/2016/04/14/Bit-Manipulation/#more