您的位置:首页 > 其它

【LeetCode】136 & 137 & 260 - Single Number I & II &III

2015-09-29 14:40 471 查看
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?

Hide Tags: Hash Table Bit Manipulation

Hide Similar Problems: (M) Single Number II (M) Single Number III (M) Missing Number (H) Find the Duplicate Number

Solution : Xor

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?

Hide Tags : Bit Manipulation

Hide Similar Problems : (M) Single Number (M) Single Number III

Solution :
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: