【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 ...
随机推荐
- Java IO 体系结构
参考文章地址: http://blog.csdn.net/oracle_microsoft/article/details/2634231 Java IO体系结构看似庞大复杂,其实有规律可循,要弄清楚 ...
- 【屌丝程序的口才逆袭演讲稿50篇】第十三篇:爱迪生欺骗了我们!【张振华.Jack】
演讲稿主题:<爱迪生欺骗了我们> --作者:张振华Jack.摘抄<马云为雅虎员工的演讲稿:爱迪生欺骗了我们> 非常多人都记得爱迪生说的 ...
- there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
建表语句: create table test_table( id integer not null auto_increment primary key, stamp_created tim ...
- 3D有向包围盒与球体碰撞的算法
之前发的小游戏滚蛋躲方块中,用它来判断球体与立方体是否发生了碰撞. http://www.cnblogs.com/WhyEngine/p/3350012.html 现在发布下该算法: 有向包围盒OBB ...
- mysql09---sql语句优化
Sql语句本身的优化 问题是: 如何从一个大项目中,迅速的定位执行速度慢的语句. (定位慢查询) 首先我们了解mysql数据库的一些运行状态如何查询(比如想知道当前mysql运行的时间/一共执行了多少 ...
- html 空白汉字占位符
可以看作一个空白的汉字 == 普通的英文半角空格 == == == no-break space (普通的英文半角空格但不换行) == 中文全角空格 (一个中文宽度) == ...
- C++和.net的集合类对应
Here's what I've found (ignoring the old non-generic collections): Array - C array, though the .NE ...
- [NPM] Execute npx commands with $npm_ Environment Variables
We will incorporate npm specific environment variables when executing various npx commands. In our e ...
- [React GraphQL] Pass Parameters to urql's useQuery React Hook
Showing how to use 'uqrl' library to do GraphQL in React. import React, {useState} from 'react' impo ...
- 在陌生Linux环境查看Tomcat服务的方法
1.查看Tomcat进程 执行命令$ps -ef|grep tomcat 你就能找出tomcat占据的进程号,当然这要求tomcat启动了. # ps -ef | grep tomcatroot ...