136. Single number
Given an array of integers, every element appears twice except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Subscribe to see which companies asked this question
Hide TagsHash Table Bit manipulationHide Similar Problems(m) Single number II (m) Single number III (m) Missing number (H) Find the Duplicate number
Public class Solution { publicint singlenumber (int[] nums) { int XOR = 0; for (int n:nums) ^= N; return xor; }}
137. Single Number II
Given an array of integers, every element appears three times except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Subscribe to see which companies asked this question
Hide TagsBit ManipulationHide Similar Problems(m) single number (m) single number III... 260. Single number IIIGiven An array of numbers
nums
, in which exactly-elements appear only once and all of the other elements appear exactly twice. Find the 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. The above example, is
[5, 3]
also correct.
- Your algorithm should run in linear runtime complexity. Could implement it using only constant space complexity?
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.
Subscribe to see which companies asked this question
Hide TagsBit ManipulationHide Similar Problems(m) single number (m) single number II
Public classSolution { Public int[] Singlenumber (int[] nums) { //Pass 1://Get The XOR of the numbers we need to find intdiff = 0; for(intnum:nums) {diff^=num; } //Get the rightmost different bit between these the numbers.Diff &=-diff; //Pass 2: int[] RETs = {0, 0};//split the numbers we is looking for into the groups. for(intnum:nums) { if(num & diff) = = 0)//The group has rightmost bit unset.{rets[0] ^=num; } Else //The group has rightmost bit set.{rets[1] ^=num; } } returnRETs; }}
136. Single number && 137. Single number II && 260. Single number III