感谢:http://www.cnblogs.com/changchengxiao/p/3413294.html

Single Number

Total Accepted: 103007 Total Submissions: 217483 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?

琢磨了很久没找到满足题目的两个要求:O(1)和不开空间。实在没办法才上网查找,原来是汇编语言的知识,而且C++中还可以直接用。

原文:

o(n)的算法只能是线性扫描一遍,可能的相法是位运算。对于异或来说:

. 异或运算是可交换,即 a ^ b = b ^ a

.  ^ a = a

那么如果对所有元素做异或运算,其结果为那个出现一次的元素,理解是a1 ^ a2 ^ ....

所以说,这是终极解法。

 public class Solution {
public int singleNumber(int[] A) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(A == null || A.length == ){
return ;
}
int result = A[]; for(int i = ; i < A.length; i++){
result ^= A[i];
}
return result;
}
}

LeetCode很有趣,希望明天可以自己想出来!

【03_136】Single Number的更多相关文章

  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. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

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

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

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

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

  5. 【Leetcode】 - Single Number II

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

  6. 【Leetcode】【Medium】Single Number II

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

  7. 【leetcode79】Single Number III

    题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in wh ...

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

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

  9. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

随机推荐

  1. ORACLE8.07客户端配置指南

    —本地机器网络连通配置 1.点击“开始”-〉“程序”菜单. 2.选择“Oracle-OracleHome81”-〉“Net Administrator”->“Net8 Configuration ...

  2. safari渲染Transition动画不流畅问题

    用css3的transition过渡来做页面动画的时候,发现在chrome和ff流畅,在safari 不流畅: 度娘找到了淘宝UED的一个类似解决方案,动画就流畅了. 测试环境: win7 32bit ...

  3. maven仓库有jar包,还是找不到类

    开始,网上的所有方法都没用. 我用的eclipse-32位的,jdk也是.然后今天换了个sts和jdk.64位的.然后就没有那个问题了.

  4. LeftoverDataException.

    在用java poi 3.8操作excel的时候,在打开一个已有excel文件时,有时候会报错: org.apache.poi.hssf.record.RecordInputStream$Leftov ...

  5. c++子类调用基类方法的一个例子

        Base.h #pragma once   class Base { public:     Base(void);     ~Base(void);     bool CreatClone( ...

  6. asp.net下载文件方法

    /// <summary> /// 下载 /// </summary> /// <param name="url"></param> ...

  7. codeforces 360 C - NP-Hard Problem

    原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the  ...

  8. Visual Studio 换皮肤

    通过字体和颜色修改 Visual Studio 提供了修改配色的入口,你完全可以根据自己的喜好进行自定义,下面就通过该方法把编辑器背景设置成 “豆沙绿”. 选择 工具 / 选项 / 环境 / 字体和颜 ...

  9. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  10. BES

    自主开发一套消息中间件系统. 需求: 1.保证能在大规模分布式环境下发送接收消息. 2.消息发送者(Producer)能够简单.容易的发送Event. 3.所有的Event都能被注册监听该Event的 ...