题目描述:

给定一个数组,里面除了一个数字,其他的都出现三次。求出这个数字

原文描述:

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?

Subscribe to see which companies asked this question

思路:

  • 设置一个32位的数组,然然后对数组,Array【i】代表i位的1个数,统计整个数组nums【】的数字,对于每个Array【i】做mod3的运算
  • 遍历nums【】数组时候,是左移&1取出,加到Array【i】上面,然后mod3
  • 最后右移加到result上面

代码:

// Single Number II
// 方法1,时间复杂度O(n),空间复杂度O(1)
public class Solution {
    public int singleNumber(int[] nums) {
        final int W = Integer.SIZE; // 一个整数的bit数,即整数字长
        int[] count = new int[W];  // count[i]表示在在i位出现的1的次数
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < W; j++) {
                count[j] += (nums[i] >>> j) & 1;
                count[j] %= 3;
            }
        }
        int result = 0;
        for (int i = 0; i < W; i++) {
            result += (count[i] << i);
        }
        return result;
    }
};

更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

【leetcode78】Single Number II的更多相关文章

  1. 【题解】【位操作】【Leetcode】Single Number II

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

  2. 【Leetcode】 - Single Number II

    Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...

  3. 【Leetcode】【Medium】Single Number II

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

  4. 【leetcode】Single Number II (medium) ★ 自己没做出来....

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

  5. 【leetcode】Single Number II

    int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  8. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  9. 【03_136】Single Number

    感谢:http://www.cnblogs.com/changchengxiao/p/3413294.html Single Number Total Accepted: 103007 Total S ...

随机推荐

  1. java中的构造,封装

    今天给大家讲一下面向对象中的构造,封装: 构造:构造方法有以下几个特点:1.方法名和类名一致.2.无返回类型.接下来的几种构造样式,直接上代码吧: //这是一个宠物类 有一个属性:名字(name) p ...

  2. Linux下DIR,dirent,stat等结构体详解

    摘自:http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/ 最近在看Linux下文件操作相关章节,遇到了这 ...

  3. URL重定向漏洞,python打造URL重定向漏洞检测脚本

    前言: 今天学习了重定向漏洞,这个漏洞比较好理解 漏洞名:URL重定向漏洞 威胁:低 漏洞的来源:开发者对head头做好对应的过滤和限制 例子: 有漏洞的网站:http://a.com/x.php?u ...

  4. CF | Alyona and Mex

    Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyon ...

  5. CommonsChunkPlugin相关

    参考https://webpack.js.org/guides/caching/#deterministic-hashes var path = require("path"); ...

  6. 20160224.CCPP体系详解(0034天)

    程序片段(01):指针与结构体.c 内容概要:指针与结构体 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include < ...

  7. PGM:贝叶斯网的参数估计2

    http://blog.csdn.net/pipisorry/article/details/52599321 没时间看了,下次再看... 具有共享参数的学习模型 全局参数共享 局部参数共享 具有 共 ...

  8. Swift中使用NSLog的问题

    在Swift中如果想要以如下方式执行NSLog,则编译器必定抱怨: class Foo {} NSLog("%@",Foo()) 因为Foo是原生Swift类不是继承自NSObje ...

  9. Java基本语法-----java流程控制语句

    1顺序语句 语句:使用分号分隔的代码称作为一个语句. 注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句. 顺序语句就是按照从上往下的顺序执行的语句. 2判断(if-else) 在我们找 ...

  10. Java基本语法-----java注释

    1注释的作用 通过注释提高程序的可读性,是java程序的条理更加清晰,易于区分代码行与注释行.另外通常在程序开头加入作者,时间,版本,要实现的功能等内容注释,方便后来的维护以及程序员的交流. 2注释的 ...