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. Mysql 数字类型转换函数

    .将Int 转为varchar经常用 concat函数,比如concat(,' .将varchar 转为Int 用 cast(a as signed) a为varchar类型的字符串 总结:类型转换和 ...

  2. 《深入Linux内核》 UNIX的一些故事

    Unix文件的类型1.普通文件2.目录3.符号链接4.面向块的设备文件5.面向字符的设备文件6.管道和命名管道7.套接字 点评:不明觉厉 打开文件进程只能访问“打开的”文件.为了打开一个文件,进程调用 ...

  3. [LeetCode#154]Find Minimum in Rotated Sorted Array II

    The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...

  4. POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)

    题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...

  5. hdu-2586-How far away ?(离线LCA)

    题意: 给定一棵树,每条边都有一定的权值,q次询问,每次询问某两点间的距离. 分析: 这样就可以用LCA来解,首先找到u, v 两点的lca,然后计算一下距离值就可以了. 这里的计算方法是,记下根结点 ...

  6. Linux Shell编程(3)——运行shell脚本

    写完一个脚本,你能够运行它用命令:sh scriptname, [5] 另外也也可以用bash scriptname. 来执行(不推荐使用:sh <scriptname, 因为这样会禁止脚本从标 ...

  7. sed的选项与命令简要

    第一部分:sed命令选项 sed选项 说明 -n, --quiet, --silent 静默模式,取消将模式空间中的内容自动打印出来. -e script, --expression=script 以 ...

  8. 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12173  Solved: 4354[Submit][Sta ...

  9. 【无源汇上下界最大流】SGU 194 Reactor Cooling

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...

  10. 最近 弄了一个 discuz 7.2,管理员发帖 会出现 很多错误,解决 办法 在别的帖子中找到了

    公司弄了一个网站,是php 的 还要有 论坛,然后配置论坛配置 了 好几天,然后好不容易配置上了,发现以管理员 发帖 的时候, 页面就会出现 各种错误,各种 警告 解决办法 是,在bbs  文件夹 里 ...