I.
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?
class Solution { public: int singleNumber(vector<int>& nums){ int ret=0; //it must be initialized for(int i=0;i<nums.size();i++){ ret=ret^nums[i]; } return ret; } };
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. Could you implement it without using extra memory?
【LeetCode】136 & 137 & 260 - Single Number I & II &III
原文:http://www.cnblogs.com/irun/p/4846294.html