Leetcode 136 Single Number I

原创 2018年04月15日 15:37:20

1.题目链接:https://leetcode.com/problems/single-number/description//
---------------------------------------------------------------------------------

2.题目大意:在一个数组里除了一个数出现一次以外,其他数都出现过两次,求这个只出现一次的这个数是多少,同时要求时间复杂度为O(N),不要申请其他内存
-------------------------------------------------------------------------------------------------------

3.思路:
这题可以说是相当巧妙,由于出现两次,用二进制位运算的异或XOR,正好为0,最后异或的结果是那个只出现一次的数

注:异或符号:^
这个可以作为一类问题的通解,关于出现两次和只出现一次的情况

--------------------------------------------------------------------------------------------
4.AC代码
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int len=nums.size(),i,result=0;
        for(i=0;i<len;i++)                   //由于出现两次,自己和自己进行异或运算结果为0000,只有出现一次的在和0000进行异或运算时为自己
        {
            result^=nums[i];
        }
        return result;
        
    }
};

Number类

-
  • 1970年01月01日 08:00

LeetCode 136:Single Number I

Single Number Given an array of integers, every element appears twice except for one. Find tha...
  • sunao2002002
  • sunao2002002
  • 2015-05-12 19:51:37
  • 3358

LeetCode 136. Single Number 题解(C++)

Given an array of integers, every element appears twice except for one. Find that single one.
  • prayyojay
  • prayyojay
  • 2016-09-29 12:07:25
  • 412

LeetCode-Easy部分标签为HashTable#136 Single Number

整数数组,除了一个出现一次外,其他都是两次。求这个唯一出现一次的数。给出了HashSet和异或操作两种算法。...
  • daigualu
  • daigualu
  • 2017-04-02 10:12:07
  • 742

python--leetcode136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one. Not...
  • wenqiwenqi123
  • wenqiwenqi123
  • 2017-10-08 19:54:07
  • 479

136. Single Number [medium] (Python)

题目链接https://leetcode.com/problems/single-number/题目原文 Given an array of integers, every element app...
  • coder_orz
  • coder_orz
  • 2016-07-29 14:30:31
  • 1699

LeetCode 136 — Single Number(C++ Java Python)

题目:http://oj.leetcode.com/problems/single-number/ Given an array of integers, every element appears ...
  • dragon_dream
  • dragon_dream
  • 2014-02-20 15:43:27
  • 4018

LeetCode[136] Single Number 位运算异或,HashMap(Java)

Problem: Given an array of integers, every element appears twice except for one. Find that single ...
  • u013810234
  • u013810234
  • 2015-05-29 10:28:50
  • 1010

[leetcode-136]Single Number(c)

问题描述: Given an array of integers, every element appears twice except for one. Find that single one....
  • zdavb
  • zdavb
  • 2015-08-14 18:55:07
  • 381

【LeetCode-Python】136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.Note: ...
  • M__D_Luffy
  • M__D_Luffy
  • 2016-10-19 22:55:35
  • 293
收藏助手
不良信息举报
您举报文章:Leetcode 136 Single Number I
举报原因:
原因补充:

(最多只允许输入30个字)