题目

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代码

class Solution {
public:
/*利用算法库的排序实现,复杂度为O(nlogn)*/
//int singleNumber(vector<int>& nums) {
// if (nums.empty())
// return -1; // sort(nums.begin(), nums.end()); // int size = nums.size(); // for (int i = 0; i < size - 2; i+=3)
// {
// if (nums[i] != nums[i + 1])
// return nums[i];
// }
// return nums[size - 1];
//} /* 方法二:
* int 数据共有32位,可以用32变量存储这 N 个元素中各个二进制位上 1 出现的次数,
* 最后 在进行 模三 操作,如果为1,那说明这一位是要找元素二进制表示中为 1 的那一位。
* 时间:O(32*N),这是一个通用的解法,如果把出现3次改为 k 次,那么只需模k就行了
*/
//int singleNumber(vector<int>& nums) {
// if (nums.empty())
// return -1;
// vector<int> bitnum(32, 0);
// int res = 0 , size = nums.size();
// for (int i = 0; i < 32; i++){
// for (int j = 0; j < size; j++){
// bitnum[i] += (nums[j] >> i) & 1;
// }
// res |= (bitnum[i] % 3) << i;
// }
// return res;
//}
/* 方法三:
* 利用三个变量分别保存各个二进制位上 1 出现一次、两次、三次的分布情况,最后只需返回变量一。
*/ int singleNumber(vector<int>& nums) {
if (nums.empty())
return -1;
int size = nums.size();
int one = 0, two = 0, three = 0;
for (int i = 0; i < size; i++){
two |= one&nums[i];
one ^= nums[i];
//cout<<one<<endl;
three = one&two;
one &= ~three;
two &= ~three;
}
return one;
}
};

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. 解决XP“不是有效Win32程序” 不是改Platform toolset

    背景 最近在写一个窗口程序,想在Windows XP上也能跑.先用vs 2015的App Wizard生成了一个实例窗口程序,按照网上大部分攻略,将 "Properties - Genera ...

  2. 基于.net core微服务(Consul、Ocelot、Docker、App.Metrics+InfluxDB+Grafana、Exceptionless、数据一致性、Jenkins)

    1.微服务简介 一种架构模式,提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相沟通(RESTfu ...

  3. Windows下打开某些软件时显示显卡驱动不是最新的问题

    在Windows下打开某些对显卡要求比较高的软件时,会出现某些显卡驱动不是最新,要求更新到最新的提示,但是当你真的去更新显卡驱动的时候,却发现现在的显卡驱动已经是最新了,那么为什么还会有这样的提示呢, ...

  4. Dubbo理论知识

    本文是作者根据官方文档以及自己平时的使用情况,对 Dubbo 所做的一个总结.如果不懂 Dubbo 的使用的话,可以参考我的这篇文章<超详细,新手都能看懂 !使用SpringBoot+Dubbo ...

  5. mongodb 上限集合

    上限集合是固定大小的循环集合按照插入以支持高性能的创建,读取和删除操作.通过循环,这意味着,当分配给该集合中的固定大小要用尽时,它会开始删除集合中最旧的文件而不提供任何明确的命令. 上限集合限制更新, ...

  6. StarUML安装与Win7不兼容解决

    最近在学习建模工具(StarUML)发现 其他功能一切正常 但是无法显示代码导出功能, 正常界面如下: 我的安装确没有导出功能缺少C++,C# ,Java等导出功能 解决办法:     到StarUM ...

  7. Java程序流程控制之if-else if-else

    java基础之流程控制(一)    流程控制 Flow Control :       流程控制语句是编程语言中的核心之一.可以分为 分支语句.循环语句和跳转语句.        本讲内容包括分支语句 ...

  8. Android 检查内存溢出

    工具网址:https://github.com/square/leakcanary 中文版说明地址:http://www.liaohuqiu.net/cn/posts/leak-canary-read ...

  9. 动画 iOS基础

    动画 iOS基础 1.     basic animation  基础动画 一个基础动画 在一个开始值和一个结束值之间运动   messageLabel.alpha=0.0; [UIView  ani ...

  10. JavaScript数据格式验证探讨

    1.需求 修改某个文本框数据,要求对修改后的格式做验证(必须是数字). 注:实际需求比上述复杂,为了说明问题,这里特意简化了需求(如:对修改后数据依赖条件的判断,数据入库等). 2.关于NaN的探讨( ...