Single Number

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?

解法一:用map记录每个元素的次数,返回次数为1的元素

class Solution {
public:
map<int,int> m;
int singleNumber(vector<int>& nums) {
for(int i = ; i < nums.size(); i ++)
{
if(m.find(nums[i]) == m.end())
m[nums[i]] = ;
else
m[nums[i]] = ;
} for(map<int,int>::iterator it = m.begin(); it != m.end(); it ++)
{
if(it->second == )
return it->first;
}
}
};

解法二:利用异或操作的结合律。

同一数字x异或自己结果为0.x^x=0

任何数字x异或0结果为x.x^0=x

class Solution {
public:
int singleNumber(vector<int>& nums) {
if(nums.empty())
return ;
int ret = nums[];
for(int i = ; i < nums.size(); i ++)
ret ^= nums[i];
return ret;
}
};

解法三:先排序,再遍历找出孤异元素

class Solution {
public:
int singleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
//A has at least 3 elements
if(nums[] != nums[])
return nums[];
int n = nums.size();
if(nums[n-] != nums[n-])
return nums[n-];
for(int i = ; i < n-; i ++)
{
if(nums[i] != nums[i-] && nums[i] != nums[i+])
return nums[i];
}
}
};

解法四:

位操作。不管非孤异元素重复多少次,这是通用做法。

对于右数第i位,如果孤异元素该位为0,则该位为1的元素总数为2的整数倍。

如果孤异元素该位为1,则该位为1的元素总数不为2的整数倍(也就是余1)。

换句话说,如果第i位为1的元素总数不为2的整数倍,则孤异数的第i位为1,否则为0.

(如果非孤异元素重复n次,则判断是否为n的整数倍)

class Solution {
public:
int singleNumber(vector<int>& nums) {
int count;
int result = ;
int ind = ; //mask position
int n = nums.size();
while(ind)
{
count = ;
for(int i = ; i < n; i ++)
{
if(nums[i] & ind)
count ++;
}
if(count % )
result |= ind;
ind <<= ;
}
return result;
}
};

【LeetCode】136. Single Number (4 solutions)的更多相关文章

  1. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  2. 【LeetCode】136. Single Number

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

  3. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  4. 【LeetCode】137. Single Number II (3 solutions)

    Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...

  5. 【一天一道LeetCode】#136. Single Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】137. Single Number II

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

  7. 【LeetCode】9. Palindrome Number (2 solutions)

    Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...

  8. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  9. 【LeetCode】260. Single Number III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 题目地址:https://leet ...

随机推荐

  1. C# 编程指南

    此部分详细介绍了 C# 语言主要功能,以及通过 .NET Framework 可以在 C# 中使用的功能. 阅读此部分的大部分内容的前提是,你已对 C# 和一般编程概念有一定的了解. 如果完全没有接触 ...

  2. PHP自己定义安装

    ① 自己定义安装(先要在管理里停止apache服务,再卸载apache.再安装时不须要重新启动电脑) apache+php+mysql+phpmyadmin自行安装 我们建议大家,安装的时候安装到同一 ...

  3. C语言:字符串输出流输出文件中的数据。

    #include<stdio.h> #include<string.h> int main() { //定义文件指针 FILE *f = NULL; //打开文件 f = fo ...

  4. [10] 圆管(Pipe)图形的生成算法

    顶点数据的生成 bool YfBuildPipeVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...

  5. Sort List leetcode java

    题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...

  6. Android -- 拷贝assets下的资源文件到SD卡中(可以超过1M)

    很多手机游戏,在安装APK之后都得需要下载相应的资源包,然后才能进入游戏. 有这样一个需求:就是游戏中需要的资源包打在APK内,随apk一起进行安装到手机中. 这样就不需要,在安装APK之后,去下载资 ...

  7. linux中sort命令

    功能说明:将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序. 参 数: -b 忽略每行前面开始出的空格字符. -c 检查文件是否已经按照顺序排序. -d 排序时,处理英文字母.数 ...

  8. java编程思想---对象

    一.对象 对于每种语言来说,都有自己操纵内存中元素的方法. 在java中,一切被视为对象.可是操纵对象的是一个"引用".举个样例,能够比作为遥控器对电视的操作,遥控器就是引用,而电 ...

  9. Thread-Local Storage for C99

    线程本地存储(TLS)是一种机制,通过这样的机制进行变量分配.在每一个现存线程都有一个实例变量.这样的执行模型GCC用来实现这个,起源于IA-64处理器,可是已经被迁移到其它的处理器.它须要大量的支持 ...

  10. ListView实现数据列表显示

    要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式: 第一种是用 ...