Single numberTotal accepted:103745 Total submissions:218647 difficulty:medium
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?
(m) Single number II (m) Single number III (m) Missing number (H) Find the Duplicate number
class solution { public : int Singlenumber ( Vector<int >& Nums) { int res = 0 ; int nums_size = Nums.size (); for (int i= 0 ; I<nums_size;i++) {res ^= Nums[i]; return res; }};
Next challenges: (m) Single number II (m) Single number III single number IITotal accepted:69333 Total submissions:191725 difficulty:medium
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?
Count the number of 1 in each bits, this method is suitable for this type of topic: All the numbers in the array appear K, only one appears once
Const intBITS =sizeof(int) *8;classSolution { Public: intSinglenumber (vector<int>&nums) { inttimes[bits]={0}; cout<<BITS<<Endl; intNums_size =nums.size (); for(intI=0; i<nums_size;i++){ intx =Nums[i]; for(intj=0; j<bits;j++){ if((X>>J) &1) {Times[j]++; } } } intres =0; for(intI=0; i<bits;i++){ if(times[i]%3) {res+=1<<i; } } returnRes; }};
Single number IIITotal accepted:18186 Total submissions:44516 difficulty:medium
Given An array of numbers nums
, in which exactly-elements appear only once and all 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
All results differ or are left with a non-0 value, which is the difference or result of only one occurrence of two digits, the number of the first bit=1 in this number, and 1 left to move so many digits into the array of the splitter.
Const intBITS =sizeof(int)*8;classSolution { Public: Vector<int> Singlenumber (vector<int>&nums) { intNums_size =nums.size (); intn =0; for(intI=0; i<nums_size;i++) {n^=Nums[i]; } intSeprator =0; for(intI=0; i<bits;i++){ if((n>>i) &1) {Seprator=1<<i; Break; }} vector<int>Res; intres1=0, res2=0; for(intI=0; i<nums_size;i++){ if((Seprator &Nums[i])) {Res1^=Nums[i]; }Else{res2^=Nums[i]; }} res.push_back (RES1); Res.push_back (Res2); returnRes; }};
Next challenges: (E) Number of 1 Bits (m) Bitwise and of Numbers Range (m) Maximum Product of Word Lengths
Single Number,single number II