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. 两款商业拓扑发现软件siteview和ElementSentry的比较

    今天在公司试用了一下两款商业拓扑发现软件游龙科技的siteview和速方软件ElementSentry. 条目/产品 速方软件ElementSentryv5.0 游龙科技Siteview NNM v3 ...

  2. 「Poetize7」足球比赛

    描述 Description SJZEZ和TSYZ正在进行一轮足球联谊赛,根据规则,这轮比赛有两场,一场在SJZEZ的主场进行,一场在TSYZ的主场进行.胜负判断标准如下:1.在两场比赛中进球总数较多 ...

  3. Unity Dx9 Occlusion Query plugin

    //Occlusion Unity plugin #include "UnityPluginInterface.h" #include <math.h>#include ...

  4. c++转换构造函数和类型转换函数

    看stl源码时,有一段代码感觉很奇怪 iterator begin() { return (link_type)((*node).next); } iterator和link_type是两种不同类型, ...

  5. [Hibernate] 基本增删查改

    本文记录,Java 应用通过 Hibernate 对数据库 MySQL 进行基本的增删改查操作,即CRUD. 本例子的目录结构如下 hibernate.cfg.xml 存储数据库信息,如数据库类型,账 ...

  6. hdu 3409 最短路树+树形dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3409 参考博客:http://www.cnblogs.com/woaishizhan/p/318981 ...

  7. N - Tram - poj1847(简单最短路)

    题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个 ...

  8. XDocument和XmlDocument的区别

    刚开始使用Xml的时候,没有注意到XDocument和XmlDocument的区别,后来发现两者还是有一些不同的. XDocument和XmlDocument都可以用来操作XML文档,XDocumen ...

  9. Node.js学习(7)----包

    包是在模块的基础上更深一步的抽象,Node.js的包类似于C/C++函数库或者Java/.NET的类库.它将独立的功能封装起来用于发布.更新.依赖管理和版本控制. Node.js的包是一个目录,其中包 ...

  10. Android 解屏幕锁与点亮屏幕(来电时效果)

    PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE); //获取电源管理器对象 PowerManager.Wak ...