【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 ...
随机推荐
- 【BZOJ】【2120】数颜色 & 【2453】维护队列
莫队算法 分块大法吼 这题乍一看跟HH的项链很像啊……只是多了一个修改操作……然而我就不会做了
- MAC 10.10 开机登录无敌风火轮问题解决方式
查明是第三方输入法引起的问题,我用的是搜狗输入法.所以把搜狗卸载就好了.(注意是卸载,不是单纯的从输入源里移除) 操作的思路是,首先要进入计算机,才干进行操作. 办法是开机进入单机模式,删除苹果一个文 ...
- Java – Top 5 Exception Handling Coding Practices to Avoid
This article represents top 5 coding practices related with Java exception handling that you may wan ...
- 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: USERS
ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 > 编辑 表空间: USERS 表空间 > 编辑 表空间: USERS 1. 一般信息返回顶部 ...
- 使用jquery操作iframe
1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id="mainiframe ...
- Informatica 常用组件Source Qualifier之七 使用排序端口
使用已排序端口时,PowerCenter 将添加端口至默认查询中的 ORDER BY 子句.PowerCenter Server 将添加配置的端口号,从源限定符转换的顶部开始.在映射中包括以下任何转换 ...
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- Math Number 数值类 包装类 数学计算 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- eclipse maven 报错Could not get the value for parameter encoding for plugin execution default
问题描述:更改默认的maven仓库路径完成后.即存maven项目或者新建maven项目的时候出现如下错误 Could not get the value for parameter encoding ...
- 在web项目启动时执行某个方法
在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...