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. [Git] An efficient GIT workflow for mid/long term projects

    reference : http://fle.github.io/an-efficient-git-workflow-for-midlong-term-projects.html Our full-w ...

  2. NLP十大里程碑

    NLP十大里程碑 2.1 里程碑一:1985复杂特征集 复杂特征集(complex feature set)又叫做多重属性(multiple features)描写.语言学里,这种描写方法最早出现在语 ...

  3. java 中的resultset的类型

    结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等. 结果集读取数据 ...

  4. CentOS 7 中 hostnamectl 的使用

    hostnamectl 是在 centos7 中新增加的命令,它是用来修改主机名称的,centos7 修改主机名称会比以往容易许多. 用法 # hostnamectl -h -h --help 显示帮 ...

  5. 笔记本wifi热点设置好后,手机连上但不能上网问题

    这个问题我遇到过,我的原因是因为电脑上装有安全防护软件360的原因 解决方法是:打开360-->找到功能大全中的流量防火墙-->打开局域网防护-->关闭局域网隐身功能,立刻解决了这个 ...

  6. Grunt学习一:使用grunt创建jquery plugin.

      下载安装github客户端. http://windows.github.com 打开github客户端. 登录后,点击左上角的加号,可以创建一个Repository. (也可以点击Clone,将 ...

  7. [Javascript] Coding interview problem: Scheduler functional way

    Implement a job scheduler which takes in a function f and an integer n, and calls f after nmilliseco ...

  8. [Backbone]4. Model & View, toggle between Model and View. -- 1

    如上图所示: Server有Data都交给Models处理, 然后由Models给Views Data,让View去告诉DOM如何显示, 然后DOM显示HTML; View events update ...

  9. nginx学习笔记(四)-----日志切割脚本及定时任务

    一.日志切割脚本 #!/bin/sh #nginx目录 BASE_DIR=/usr/local/nginx #生成的日志 BASE_FILE_NAME=jonychen.access.log CURR ...

  10. Socket 之 传值方式

    一.传送 DataTable 和 DataSet 1.发送端需序列化数据为二进制序列化格式 public static byte[] GetBinaryFormatDataSet(DataSet ds ...