首页 > 其他 > 详细

136. Single Number leetcode做题报告

时间:2017-01-15 18:24:46      阅读:265      评论: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) {

        for(int i=1;i<nums.size();i++){

            nums[0]^=nums[i];

        }

        return nums[0];

    }

};


要求O(n)并且不用额外的变量。
非常巧妙的技巧。相同数取异或为0,所0和所有数的异或为本身,所以最后剩下的就是Single Number。


136. Single Number leetcode做题报告

原文:http://zjwzjw369.blog.51cto.com/10388875/1892102

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!