[Leetcode] Single Number
2016-12-18 21:51
302 查看
描述
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?
分析
这道题跟 Missing Number 的第二种做法很像,直接对所有数进行异或,时间复杂度 O(n) ,空间复杂度 O(1) ,代码如下。代码
class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for (int i:nums) res = res ^ i; return res; } };
相关文章推荐
- [leetcode] Single Number
- Leetcode Note:1.Single Number问题
- LeetCode 136. Single Number
- LeetCode 136 Single Number (异或)
- LeetCode: Single Number
- LeetCode-136 Single Number
- [LeetCode] 102: Single Number
- LeetCode - Single Number
- [Leetcode]Single Number
- leetcode:Single Number 【Java】
- LeetCode 136 Single Number(仅仅出现一次的数字)
- leetcode 136. Single Number
- [leetcode代码]Single Number
- LeetCode--No.136--Single Number
- LeetCode 136 Single Number
- LeetCode:Single Number
- leetcode -- Single Number
- Leetcode: Single Number
- Leetcode 136. Single Number
- 【leetcode】Single Number