题目描述:

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

原文描述:

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. MacOS下postgresql数据库密码的那些事

    如果你是第一次玩postgresql数据库,你会发现你给role或者user明明设置了密码,但在登录的时候毛都不用输入,直接就进去了,怎么那么爽快!? 虽然爽快,但貌似不该这样啊. 其实这些都和一个重 ...

  2. ROS机器人程序设计(原书第2版)补充资料 kinetic

    Effective Robotics Programming with ROS Third Edition Find out everything you need to know to build ...

  3. 在Spring Boot中使用数据缓存

    春节就要到了,在回家之前要赶快把今年欠下的技术债还清.so,今天继续.Spring Boot前面已经预热了n篇博客了,今天我们来继续看如何在Spring Boot中解决数据缓存问题.本篇博客是以初识在 ...

  4. 修改hosts立刻生效不必重启

    有时我们会通过修改Hosts文件(路径为系统盘:\WINDOWS\system32\drivers\etc\hosts),在修改并保存Hosts文件后需要重启才能使设置生效. 这时可以打开命令提示符 ...

  5. JVM概述

    JVM是什么 JVM全称是Java Virtual Machine(java虚拟机).它之所以被称之为是"虚拟"的,就是因为它仅仅是由一个规范来定义的抽象计算机.我们平时经常使用的 ...

  6. Android初级教程:RatingBar的使用

    记得淘宝里面买家给卖家评分的时候会有一个星星状的评分条,其实就是基于RatingBar做了自定义使用了.那么本篇文章就对RatingBar的使用做一个基本的认识. 接下来就是正题,那就是对于Ratin ...

  7. TraceView性能分析工具介绍

    一.TraceView简介 TraceView是AndroidSDK里面自带的工具,用于对Android的应用程序以及Framework层的代码进行性能分析. TraceView是图形化的工具,最终它 ...

  8. windows下实现win32俄罗斯方块练手,编程的几点心得

    编程珠玑2阅读笔记: 1.使用c语言性能监视器,完成对代码的调优工作 2.关联数组:  拓扑排序算法,可以用于当存在遮挡的时候决定三维场景的绘制顺序. 3.小型算法中的测试与调试工具 脚手架程序:&l ...

  9. 06 Activity OnNewIntent方法

    OnNewIntent方法:该方法体现在Activity的启动模式上 如sigleTop上: X这个Activity启动模式为sigleTop,Y这个Activity启动模式为stdanderd 那么 ...

  10. Dynamics CRM2015 Custom Code Validation Tool工具的使用

    工具下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=45535 下载后双击exe文件解压后里面会有个zip文件,将文件导入系 ...