<LeetCode>136. Single Number

来源:互联网 发布:淘宝导航怎么设置颜色 编辑:程序博客网 时间:2024/06/17 01:46
这是原题描述:

136. Single Number

 
 My Submissions
  • Total Accepted: 136646
  • Total Submissions:269614
  • Difficulty: Medium

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?

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems













思路解析:

一开始我也想的是一个个查找,想了下时间复杂度应该很高,应该会超时,就在想其他方法。

结果博主太笨,就不知道怎么写了。。。。百度了下,看到别人用的异或运算,自己简直懵比,,任何就看了好久的异或运算,才弄明白到底怎么回事。

(关于异或运算,可以看我另一篇文章哟)

大概的思路就是下面的代码,,很简单,主要是异或运算的理解。

下面是我的代码:

class Solution {public:    int singleNumber(vector<int>& nums) {        int t=0;        int len=nums.size();        for(int i=0;i<len;i++)        {            t=t^nums[i];        }        return t;    }};


0 0