题目:

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?

要求O(n)算法,还不能有辅助空间。开始用了各种O(n^2)的方法,都超时,后来突然顿悟了。异或可以消掉两个相同数字。

直接把所有数字异或在一起,就是单独的那个数字了。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public://异或可以把两个相同的数字消除 O(n) AC
int singleNumber3(int A[], int n) {
int ans = ;
for(int i = ; i < n; i++)
{
ans ^= A[i];
}
return ans;
}
}; int main()
{
Solution s;
int a[] = {,,,,,,,,};
int n = s.singleNumber3(a, ); return ;
}

【leetcode】Single Number (Medium) ☆的更多相关文章

  1. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  2. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  3. 【题解】【位操作】【Leetcode】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. 【Leetcode】 - Single Number II

    Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...

  5. 【leetcode】Single Number II (medium) ★ 自己没做出来....

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  6. 【Leetcode】Single Number

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

  7. 【leetcode】Single Number II

    int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. [转]Informatica vs SSIS

    转自 http://blog.csdn.net/thy822/article/details/8489779 这篇文章, 我不能同意更多, 所以转在这里. Here is my thinking af ...

  2. IOS开发之----NSDictionary,JSON和XML互相转换

    本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4044521.html,转载请注明出处.     -(void)test {     //XML文本范例   ...

  3. 使用ajax获取JSON数据的jQuery代码的格式

    具体的也可以参考:http://www.w3cfuns.com/notes/16039/2b004b1bcdf79092f2e66b2bbe9f51df.html

  4. MAVEN for mac 安装

    http://blog.csdn.net/anialy/article/details/22217937 下载 maven  http://mirrors.hust.edu.cn/apache/mav ...

  5. 【PHP面向对象(OOP)编程入门教程】11.类的继承

    继承作为面向对象的三个重要特性的一个方面,在面向对象的领域有着及其重要的作用,好像没听说哪个面向对象的语言不支持继承. 继承是PHP5面象对象程序设计的重要特性之一,它是指建立一个新的派生类,从一个或 ...

  6. 【PHP面向对象(OOP)编程入门教程】18.__call()处理调用错误

    在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...

  7. Code First02---CodeFirst配置实体与数据库映射的两种方式

    Code First有两种配置数据库映射的方式,一种是使用数据属性DataAnnotation,另一种是Fluent API. 这两种方式分别是什么呢?下面进行一一解释: DataAnnotation ...

  8. Unity3D在IOS上的优化小结

    http://www.58player.com/blog-635-122.html 最近一段時間一直在做Unity 在IOS設備上的資源優化,結合Unity的官方文檔以及自己遇到的實際問題,我把自己認 ...

  9. All Kind Of Conference(随时更新...)

    收集一些前端开发的各种会议,里面有视频或者PPT,随时查看都还是很有收获的.还有要向这些演讲的前辈看齐- AC 2015:http://ac.alloyteam.com/2015/ AC 2016:h ...

  10. JavaScript深入浅出2-表达式和运算符

    慕课网教程视频地址:Javascript深入浅出 表达式是指能计算出值的任何可用程序单元 原始表达式:常量.直接量 3.14,“test” 关键字 null,this 变量 i,k,j 表达式含:原始 ...