【LeetCode】136. Single Number (4 solutions)
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)的更多相关文章
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】136. Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】137. Single Number II (3 solutions)
Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...
- 【一天一道LeetCode】#136. Single Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】137. Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
- 【LeetCode】-- 260. Single Number III
问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...
- 【LeetCode】260. Single Number III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 题目地址:https://leet ...
随机推荐
- 很酷的C语言技巧
C语言常常让人觉得它所能表达的东西非常有限.它不具有类似第一级函数和模式匹配这样的高级功能.但是C非常简单,并且仍然有一些非常有用的语法技巧和功能,只是没有多少人知道罢了. 指定的初始化 很多人都知道 ...
- Furure的简单介绍和使用
引子:
- Objective-C:NSArray的常见操作
NSArray不可变字符串的主要操作有:创建.枚举.排序.与NSString之间的相互转换 注意: NSArray可以存对象,不可以存基本数据类型.结构体.数组.指针.nil.NULL NSArray ...
- 我所遭遇过的游戏中间件---HumanIK
我所遭遇过的游戏中间件---HumanIK Autodesk HumanIK游戏中间件,为游戏创建更加可信.真实的角色动画.该中间件的全身逆向运动(FBIK)系统支持角色真实地与所在环境及其它角色进行 ...
- JavaScript中将html字符串转化为Jquery对象或者Dom对象
实例代码: $('<a href="javascript:void(0);" onclick="showUI(this,"4028f65d5d1bb627 ...
- 遇到问题描述:Android Please ensure that adb is correctly located at问题解决
遇到问题描述: 运行android程序控制台输出 [2013-11-04 16:18:26 - ] The connection to adb is down, and a severe error ...
- 图解Eclipse或者SpringSource Tool Suite 创建模块化Maven项目
第一步:Package Explorer里选择右键,新建Maven项目,如果没有在Other里找,还没有确认一下是否安装了Maven插件 第二步:在Wizards向导里可以通过搜索,找到Maven P ...
- 如何将xml转为python中的字典
如何将xml转为python中的字典 import cElementTree as ElementTree class XmlListConfig(list): def __init__(self, ...
- javascript进行遍历
javascript进行遍历 <!doctype html> <html lang="en"> <head> <meta charset= ...
- Sqlserver存储过程生成日期维度
话不多说,之前已经有一篇日志是利用oracle的存储过程生成日期维度表,接下来我们就用sqlserver来实现这个操作,如下面的步骤所示 1:创建日期维度表(Dim_time) USE [DW] GO ...