【LeetCode】137. Single Number II (3 solutions)
Single Number II
Given an array of integers, every element appears threetimes 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记录次数
class Solution {
public:
int singleNumber(vector<int>& nums) {
map<int, int> m;
int n = nums.size();
for(int i = ; i < n; 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;
}
}
};

解法二:先排序,再遍历找出孤异元素
class Solution
{
public:
int singleNumber(vector<int>& nums)
{
int n = nums.size();
sort(nums.begin(), nums.end());
//note that at least 4 elements in nums
if(nums[] != nums[])
return nums[];
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的元素总数为3的整数倍。
如果孤异元素该位为1,则该位为1的元素总数不为3的整数倍(也就是余1)。
换句话说,如果第i位为1的元素总数不为3的整数倍,则孤异数的第i位为1,否则为0.
(如果非孤异元素重复n次,则判断是否为n的整数倍)
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ret = ;
int mask = ;
while(mask)
{
int countOne = ; //number of digit 1
for(int i = ; i < nums.size(); i ++)
{
if(nums[i] & mask)
countOne ++;
}
if(countOne % == )
ret |= mask;
mask <<= ;
}
return ret;
}
};

【LeetCode】137. Single Number II (3 solutions)的更多相关文章
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】137. Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 【一天一道LeetCode】#137. Single Number II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【LeetCode】136. Single Number (4 solutions)
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- 【LeetCode】136. Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【Leetcode】264. Ugly Number II ,丑数
原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...
随机推荐
- Hashing图像检索源码及数据库总结
下面的这份哈希算法小结来源于本周的周报,原本并没有打算要贴出来的,不过,考虑到这些资源属于关注利用哈希算法进行大规模图像搜索的各位看官应该很有用,所以好东西本小子就不私藏了.本资源汇总最主要的收录原则 ...
- 计算Fisher vector和VLAD
This short tutorial shows how to compute Fisher vector and VLAD encodings with VLFeat MATLAB interfa ...
- spring中ApplicationContextAware接口使用理解
一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...
- C语言:返回两个数组中第一个元素的指针,并输出这个值
// // main.c // Pointer_search // // Created by ma c on 15/8/2. // Copyright (c) 2015年. All righ ...
- 谈谈javascript的函数作用域
在一些类似c语言的编程语言中,花括号内的每一段代码都具有各自的作用域,而且变量在声明他们的代码段之外是不可见的,我们称为块级作用域(block scope),而javascript中没有块级作用域.取 ...
- 俄罗斯水手 [C#] 对List<T>取交集、连集及差集
※本文使用int為例,若為使用自訂之DataModel,需實作IEquatable<T>介面才能使用 1. 取交集 (A和B都有) List A : { 1 , 2 , 3 , 5 , ...
- 五条强化 SSH 安全的建议
当你查看你的 SSH 服务日志,可能你会发现充斥着一些不怀好意的尝试性登录.这里有 5 条常规建议(和一些个别特殊策略)可以让你的 OpenSSH 会话更加安全. 强化密码登录 密码登录很方便,因为你 ...
- 转:好用 Eclipse插件收集与说明
http://developer.51cto.com/art/201007/214478.htm
- NGUI诡异的drawCall
看了很多关于NGUI drawCall的文章,见得比较多的一个观点是:一个 Atlas 对应一个Drawcall. 好奇心下做了个demo,两个panel中只用到一个Atlas,却产生了10个draw ...
- System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...