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)的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  2. 【LeetCode】137. Single Number II

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

  3. 【一天一道LeetCode】#137. Single Number II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  6. 【LeetCode】136. Single Number (4 solutions)

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  7. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

  8. 【LeetCode】136. Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  9. 【Leetcode】264. Ugly Number II ,丑数

    原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...

随机推荐

  1. 常用sql001_partition by 以及 row_number()和 dense_rank()和rank()区别

    create table student ( sid varchar2(10), --学号 sname varchar2(10), --姓名 classid varchar2(10), --班级号 s ...

  2. Cesium随笔(4)去掉cesium和bing地图的logo 【转】

    在开发过程中timeline上面的logo是不是有些碍眼嘞,下面写一下去掉的方法: 在style标签中添加 css: .cesium-widget-credits{   display:none!im ...

  3. iOS开发-搜索栏UISearchBar和UISearchController

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

  4. Loadrunner视频教程汇总

    小布老师视频:测试工具概述,兼LoadRunner介绍 -1-4http://www.boobooke.com/v/bbk1046http://www.boobooke.com/v/bbk1046.z ...

  5. python3 把excel文件合并并保存到csv文件

    具体是这样,某路径下有很多 excel文件,文件名中包含相同关键字的是一类文件,把包含相同关键字的文件合并成一个文件,生成一个新的csv文件 # coding=utf-8 import xlrd im ...

  6. Linux清理磁盘空间

    1.首先确定是否是磁盘满了 命令:   df -h 参数说明: -a:列出所有的文件系统,包括系统特有的/proc等文件系统 -k:以KB的容量显示各文件系统 -m:以MB的容量显示各文件系统 -h: ...

  7. STM8S AD转换

    //不说那么多了,直接上程序 void ADC1_DeInit(void) { ADC1->CSR = ADC1_CSR_RESET_VALUE; ADC1->CR1 = ADC1_CR1 ...

  8. Fusioncharts的导出图片訪问官网问题

    Fusioncharts3.5使用自带的导出功能,须要訪问官网 问题描写叙述:使用fusioncharts自带的exportchart方法来导出图片的时候.要訪问export.api3.fusionc ...

  9. PHP高级教程-JSON

    PHP JSON 本章节我们将为大家介绍如何使用 PHP 语言来编码和解码 JSON 对象. 环境配置 在 php5.2.0 及以上版本已经内置 JSON 扩展. JSON 函数 函数 描述 json ...

  10. Gradle修改Maven仓库地址

    博客已经搬家https://www.tianmingxing.com 背景 不知从什么时候大家开始使用gradle管理项目了,随着时间的推移从maven转过来的人肯定越来越多.关于gradle的优势在 ...