Given 2*n + 1 numbers, every numbers occurs twice except one, find it.

 
Example

Given [1,2,2,1,3,4,3], return 4

Challenge

One-pass, constant extra space.

题意

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。

解法一:

 class Solution {
public:
/*
* @param A: An integer array
* @return: An integer
*/
int singleNumber(vector<int> &A) {
int num = A[];
for (int i = ; i < A.size(); ++i) {
num ^= A[i];
} return num;
}
};

利用异或的性质,相同为0

82. Single Number【easy】的更多相关文章

  1. 136. Single Number【LeetCode】异或运算符,算法,java

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  2. 491. Palindrome Number【easy】

    Check a positive number is a palindrome or not. A palindrome number is that if you reverse the whole ...

  3. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  4. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  7. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  8. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  9. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

随机推荐

  1. matlab strel

    >>se3 = strel('square',3)Neighborhood: 1 1 1 1 1 1 1 1 1 >> se3 = strel('line',3 , 45)Ne ...

  2. cocob优化算法

    cocob算法是一个启发式的算法,针对SGD不需要设置learning rate. https://github.com/bremen79/cocob

  3. Java 命名小技巧

    存储信息: xxxStorage 映射: xxxMapping 通过参数获取某个对象: getxxxFor 处理器: xxxHanlder handle 检索: xxxretriever 验证器: x ...

  4. mysql之子查询

    所谓子查询,就是指在一个查询之中嵌套了其他的若干查询,通过子查询可以实现多表查询,该查询语句中可能包含IN,ANY,ALL和EXISTS等关键字,除此之外还可以包含比较运算符,子查询经常出现在WHER ...

  5. JDK 1.7版本的 新特性

    摘自: http://yanguz123.iteye.com/blog/1934766 Jdk1.7的新特性: 1,switch中可以使用字串 Java代码: String s = "tes ...

  6. 小课堂week16 编程范式巡礼第一季 三大基石

    编程范式巡礼第一季 三大基石 最近迷上了一些哲史类书籍,回望过去.放眼未来,往往沉浸在其思维之美中无法自拔.计算机编程是一门非常年轻的学科,沉淀不足也是年轻的一个侧面,在编程领域,有足够思想深度的作品 ...

  7. [Functional Programming] Function signature

    It is really important to understand function signature in functional programming. The the code exam ...

  8. python中字符串list转化为数值型

    之前在网上找相关的资料,给出的方法都不合适, 经过很长时间的试错才知道源于python2.X和python3.X的不同, 原理都是采用map函数,但是二者返回的信息不同 Python2.x,可以使用m ...

  9. Think Pad T410键盘溅水有惊无险

    昨日不小心单位给配的T410溅水了,由于水不多,用餐巾纸擦干后就晾起来了. 大概过了6小时,心想现在该好了吧,于是按开机键,无效! 当时暗骂Thinkpad给LX做坏了,一点小水都挡不住,还敢号称防洒 ...

  10. (笔试题)质数因子Prime Factor

    题目: Given any positive integer N, you are supposed to find all of its prime factors, and write them ...