class Solution {
public:
int singleNumber(int A[], int n) {
int i,j;
for(i=; i<n; i++)
{
for(j=i+; j<n; j++)
{
if(A[j]==A[i])
{
int temp = A[i+];
A[i+] = A[j];
A[j] = temp;
i++;
break;
}
}
if(j==n)
return A[i];
}
}
};

上面的是传统方法,时间复杂度是O(n2),空间复杂度是O(1)。

 class Solution {
public:
int singleNumber(int A[], int n) {
int result=;
for(int i=; i<n; i++)
result = result ^ A[i];
return result;
}
};

用位异或运算实现,时间复杂度和空间复杂度均为O(1).

运用了异或运算具有交换律和结合律的性质。

交换律: a^b = b^a

结合律: (a^b)^c = a^(b^c)

另外,a^a=0。

[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.的更多相关文章

  1. [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

    class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...

  2. LeetCode OJ 80. Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  4. LeetCode OJ -Happy Number

    题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...

  5. LeetCode OJ 33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. LeetCode OJ 26. Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. 【LeetCode OJ】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. LeetCode OJ:Number of Islands(孤岛计数)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. Linux kernel ‘net/key/af_key.c’信息泄露漏洞

    漏洞名称: Linux kernel ‘net/key/af_key.c’信息泄露漏洞 CNNVD编号: CNNVD-201307-071 发布时间: 2013-07-05 更新时间: 2013-07 ...

  2. joelonsoftware 读书摘录

    joelonsoftware 读书摘录   <五个为什么>  1.“黑天鹅难题”,代表外来因素,是一个超出正常预料之外的事件.  2.丰田佐吉的“五个为什么”,当某个地方出现问题时,你就一 ...

  3. 检测CPU是否支持虚拟化

    一:下载检测软件 地址:http://files.cnblogs.com/hongmaju/Coreinfo.rar 二:使用方法 打开运行窗口,找到Coreinfo.exe,运行如下: 现在你要做的 ...

  4. ToArray()和IEnumerable<T>,List<T>

    一:ToArray() 在程序中,我们往往习惯使用List<>这种集合类,但是程序中却要求需要传递一个数组,List<>已经为我们提供了toArray()方法 二:IEnume ...

  5. 在Kafka中修改Topic的preferred replica

    参考site:https://cwiki.apache.org/confluence/display/KAFKA/Replication+tools 目前我们的topic  test-add-repl ...

  6. java JDK安装

    JDK安装包下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 图释安装 ...

  7. Ubuntu 12.04 Server OpenStack Havana多节点(OVS+GRE)安装

    1.需求 节点角色 NICs 控制节点 eth0(10.10.10.51)eth1(192.168.100.51) 网络节点 eth0(10.10.10.52)eth1(10.20.20.52)eth ...

  8. 使用Array

    public class UsingArray {     public static void output(int[]Array)     {         if(Array!=null)    ...

  9. POJ 3280 Cheapest Palindrome(水题)

    题意:给定一个完全由小写字母组成的字符串s,对每个字母比如x(或a,b,c...z),在字符串中添加或者删除它分别需要花费c1['x']和c2['x']的代价,问将给定字符串变成回文串所需要的最少代价 ...

  10. redis 集群

    http://www.linuxidc.com/Linux/2015-08/121845.htm Redis3.0版本之后支持Cluster,具体介绍redis集群我就不多说,了解请看redis中文简 ...