题目

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. jq解析xml

    注意:url路径不能用相对路径,需要加入http协议

  2. Sql 2000系统表 语句查询表结构

     SQL2000系统表的应用  –1:获取当前数据库中的所有用户表 select Name from sysobjects where xtype=’u’ and status>=0 –2:获取 ...

  3. 转 DG switchover

    I. Pre-Switchover Checks These steps should be completed before the switchover planned maintenance w ...

  4. String的小笔记

    String类的对象是不可变的! 在使用String类的时候要始终记着这个观念.一旦创建了String对象,它就不会改变. String类中也有可以改变String中字符串的方法,但只要是涉及改变的方 ...

  5. hdu 5971 Wrestling Match 判断能否构成二分图

    http://acm.hdu.edu.cn/showproblem.php?pid=5971 Wrestling Match Time Limit: 2000/1000 MS (Java/Others ...

  6. CSS 中,用 float 和 position 的区别是什么?

    CSS 中,用 float 和 position 的区别是什么? 呃,其实这个命题有误,只有position才是定位,float不能说是定位,不过你可以说这两种布局方式有什么不同.float和posi ...

  7. PHP识别二维码功能,php-zbarcode 安装

    php-zbarcode是PHP识别二维码的扩展. 下面是安装方法,安装前要先安装ImageMagick.zbar. php-zbarcode 下载地址 安装ImageMagick: yum inst ...

  8. 仿天猫淘宝的ShopNC好商城原生Android 客户端源码项目

    开发环境:Android Studio 2.0 | Gradle 2.0.0最后更新:2016-04-28 简介:基于好商城V4的Android客户端 目前已完成的功能(概述): 1.启动页 -> ...

  9. Mac终端给命令设置别名alias的办法

    在Mac里使用curl https://www.google.com,运行后得不到期望看到的google首页的HTML source code. vi ~/.bashrc, 输入下面两行内容. 以后每 ...

  10. 给我说说你能想到几种分布式session实现

    附录: https://mp.weixin.qq.com/s/8Hh4j0CjfF5S8zM29JZl2w # 面试官心理分析 面试官问了你一堆 dubbo 是怎么玩儿的,你会玩儿 dubbo 就可以 ...