题目

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

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析

由上一题改动而来,LeetCode 136 Single Number描述的是:数组中其他数出现两次,仅有一个出现一次的,直接用所有元素异或就行了(只要是偶数次,都可以用这个方法),本题变为其他元素出现3次,而且时间复杂度要求线性,空间为常数。

用排序的方法依然可以简单解决,但是复杂度不符合线性的要求。而且,此题不能简单的用异或运算实现了。

看到了一个具有很棒实现方法的帖子:LeetCode 137 Single Number II,提供了两种方法,受益匪浅。

AC代码

  1. class Solution {
  2. public:
  3. /*利用算法库的排序实现,复杂度为O(nlogn)*/
  4. //int singleNumber(vector<int>& nums) {
  5. // if (nums.empty())
  6. // return -1;
  7. // sort(nums.begin(), nums.end());
  8. // int size = nums.size();
  9. // for (int i = 0; i < size - 2; i+=3)
  10. // {
  11. // if (nums[i] != nums[i + 1])
  12. // return nums[i];
  13. // }
  14. // return nums[size - 1];
  15. //}
  16. /* 方法二:
  17. * int 数据共有32位,可以用32变量存储这 N 个元素中各个二进制位上 1 出现的次数,
  18. * 最后 在进行 模三 操作,如果为1,那说明这一位是要找元素二进制表示中为 1 的那一位。
  19. * 时间:O(32*N),这是一个通用的解法,如果把出现3次改为 k 次,那么只需模k就行了
  20. */
  21. //int singleNumber(vector<int>& nums) {
  22. // if (nums.empty())
  23. // return -1;
  24. // vector<int> bitnum(32, 0);
  25. // int res = 0 , size = nums.size();
  26. // for (int i = 0; i < 32; i++){
  27. // for (int j = 0; j < size; j++){
  28. // bitnum[i] += (nums[j] >> i) & 1;
  29. // }
  30. // res |= (bitnum[i] % 3) << i;
  31. // }
  32. // return res;
  33. //}
  34. /* 方法三:
  35. * 利用三个变量分别保存各个二进制位上 1 出现一次、两次、三次的分布情况,最后只需返回变量一。
  36. */
  37. int singleNumber(vector<int>& nums) {
  38. if (nums.empty())
  39. return -1;
  40. int size = nums.size();
  41. int one = 0, two = 0, three = 0;
  42. for (int i = 0; i < size; i++){
  43. two |= one&nums[i];
  44. one ^= nums[i];
  45. //cout<<one<<endl;
  46. three = one&two;
  47. one &= ~three;
  48. two &= ~three;
  49. }
  50. return one;
  51. }
  52. };

GitHub测试程序源码

LeetCode(137) Single Number II的更多相关文章

  1. leetcode文章137称号-Single Number II

    #include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int cou ...

  2. LeetCode(136) Single Number

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

  3. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  4. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  5. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  6. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  7. LeetCode(202) Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  8. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  9. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

随机推荐

  1. with rollup

    实验吧的一道ctf题,这两天无聊,做做ctf题.在实验吧被一道也题卡了好久. 页面很简单就是一个登陆页面,按照之前的经验觉得应该是注入吧.再看题猜测应该是绕waf之类的. 查看页面源码找到了提供的源代 ...

  2. Java内置锁和简单用法

    一.简单的锁知识 关于内置锁 Java具有通过synchronized关键字实现的内置锁,内置锁获得锁和释放锁是隐式的,进入synchronized修饰的代码就获得锁,走出相应的代码就释放锁. jav ...

  3. jquery select 列表 ajax 动态获取数据 模糊查询 分页

    最近需要一个这样的select 在网上找的多是数据一次性获取到再通过前端模糊查询匹配的 这样在数据量比较大的情况下不适合 ,所以参考http://www.jq22.com/jquery-info145 ...

  4. [在读]JavaScript异步编程:设计快速响应的网络应用

    很棒的一本,就如书名所示,主要讲js异步的一些东西,比如定时器.Jquery的promise和deffered,node...看了一小半.推荐哦~~

  5. Railroad UVALive - 4888 记忆化搜索

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. elastcisearch中文分词器各个版本

    地址 https://github.com/medcl/elasticsearch-analysis-ik/releases?after=v6.0.1

  7. Windows server 2003 + IIS6 搭建Asp.net MVC运行环境

    安装.Net Framework4.0.下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=17718 安装WindowsServ ...

  8. js js弹出框、对话框、提示框、弹窗总结

    js弹出框.对话框.提示框.弹窗总结 一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹 ...

  9. Java开发笔记(九十六)线程的基本用法

    每启动一个程序,操作系统的内存中通常会驻留该程序的一个进程,进程包含了程序的完整代码逻辑.一旦程序退出,进程也就随之结束:反之,一旦强行结束进程,程序也会跟着退出.普通的程序代码是从上往下执行的,遇到 ...

  10. Java面试:投行的15个多线程和并发面试题(转)

    多线程和并发问题已成为各种 Java 面试中必不可少的一部分.如果你准备参加投行的 Java 开发岗位面试,比如巴克莱银行(Barclays).花旗银行(Citibank).摩根史坦利投资公司(Mor ...