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. Bash,Vim,gdb&git常用命令

    Bash 目录 pwd  //查看当前目录 mkdir dir1 dir2  //创建目录 tree dir1 mv test1.cpp test2.cpp dir1 dir  //移动文件/目录到目 ...

  2. RabbitMQ Zabbix 监控

    RabbitMQ Zabbix 监控 参考: https://github.com/jasonmcintosh/rabbitmq-zabbix copy api.py list_rabbit_node ...

  3. 使用Java、Matlab画多边形闭合折线图

    由于写论文要将“哈密顿回路问题(TSP)”的求解中间结果表示出来,查了一下使用程序画多边形图形.现在在总结一下,这个图是“由给定节点首尾相连的”闭合多边形. 1.使用matlab作闭合多边形图 没有找 ...

  4. HTTP响应状态码【总结】

    常见的状态码 [1XX]表示[消息] [2XX]表示[成功] [3XX]表示[重定向] [4XX]表示[请求错误] [5XX]表示[服务器端错误] 200:OK.请求被正常处理 204:No Cont ...

  5. 使用Spring Security和OAuth2实现RESTful服务安全认证

    这篇教程是展示如何设置一个OAuth2服务来保护REST资源. 源代码下载github. (https://github.com/iainporter/oauth2-provider)你能下载这个源码 ...

  6. C++初始化列表和大括号中构造的差别

    C++的对象构造函数有两种初始化的方法: 1.初始化列表 2.大括号中面赋值 这两种推荐使用另外一种.原因在于使用初始化列表仅仅须要进行一次初始化.而使用大括号内赋值的话首先须要调用默认构造函数初始化 ...

  7. [Node.js] Level 2 new. Event

    Chat Emitter We're going to create a custom chat EventEmitter. Create a new EventEmitter object and ...

  8. DevExpress学习03——label控件的背景色问题

    今天使用了DevExpress的labelControl,发现拖放上去,其背景色和主窗体的背景一样,非常不谐调,把BackColor设置为透明也不行(Windows中的Label可以). 没有办法,我 ...

  9. Asp.Net 之 前台绑定常用总结

    1.<%= 变量名 %> 里面放的是后台定义的变量名,如: <div> <p> <%= DateTime.Now.ToString() %></p ...

  10. POJ_3342_Party_at_Hali-Bula

    #include <iostream> #include <map> #include <cstring> using namespace std; int Gra ...