码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】136. Single Number 解题小结

时间:2016-08-31 07:08:42      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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 num = 0;
        for (int i = 0; i < nums.size(); ++i){
            num ^= nums[i];
        }
        return num;
    }
};

 

【LeetCode】136. Single Number 解题小结

标签:

原文地址:http://www.cnblogs.com/Doctengineer/p/5824385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!