题目

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. 牛客寒假6-C.项链

    链接:https://ac.nowcoder.com/acm/contest/332/C 题意: 小B想给她的新项链染色. 现在有m种颜色,对于第i种颜色,小B有a_i单位的颜料,每单位颜料可以染项链 ...

  2. hdu6314( 2018 Multi-University Training Contest 2)

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6314 ----. 又是一个数学题! 这个题使用容斥原理解决的,现场看dls推公式. 我也 ...

  3. POJ 1458 Common Subsequence DP

    http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长 ...

  4. springmvc整合elasticsearch

    网上大多时关于springboot整合的,也有spring的,但是 坑太多,都没法愉快的玩耍 这篇让我整合成功 https://www.cnblogs.com/sunny1009/articles/7 ...

  5. iOS NSDate 常用日期相关函数的封装

    Category是类别,一般情况用分类好,用Category去重写类的方法,仅对本Category有效,不会影响到其他类与原有类的关系. NSDate+Category.h 代码: #import & ...

  6. ExpandableListView 安卓二级菜单

    ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView).ExpandableListView允许有两个层次:一级列表中有二级列表.比如在 ...

  7. 绘制surfaceView 基础类

    public class SurfaceViewTempalte extends SurfaceView implements Callback, Runnable { private Surface ...

  8. Android 检查内存溢出

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

  9. jenkins SVN更改密码后出现的坑爹问题

    1.前提 公司SVN账号密码和AD账号密码是绑定在一起的,为了保证代码检出总是最新,jenkins中做代码检查前总会从SVN中检出最新代码. 最近公司要求AD账户不得使用原始密码,更改密码后,jenk ...

  10. bsub && lsf 介绍

    文章转载地址:http://www.bbioo.com/lifesciences/40-114265-1.html LSF系统介绍 http://scc.ustc.edu.cn/zh_CN/ 中科大超 ...