/**
* Source : https://oj.leetcode.com/problems/single-number-ii/
*
* 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?
*
*/
public class SingleNumber2 { /**
* 数组中每个元素出现3次,只有一个元素出现一次,找出出现一次的元素
*
* 沿用SingleNumber的方法,比如:{1,1,1,2,2,2,3}
* 01
* 01
* 01
* 10
* 10
* 10
* 11
* ______
* 44 对每一位求和
* 11 每一位对3求余
*
* @param arr
* @return
*/
public int singleNumber (int[] arr) {
int res = 0;
for (int i = 0; i < 32; i++) {
int sum = 0;
int mask = 1<<i;
for (int j = 0; j < arr.length; j++) {
if ((arr[j] & mask) != 0) {
sum ++;
}
}
res |= (sum % 3) << i;
}
return res;
} public static void main(String[] args) {
SingleNumber2 singleNumber2 = new SingleNumber2();
System.out.println(singleNumber2.singleNumber(new int[]{1,1,1,2,2,2,3}) + " == 3");
System.out.println(singleNumber2.singleNumber(new int[]{14,14,14,9}) + " == 9");
} }

leetcode — 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(找出数组中只出现一次的数2)

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

  3. LeetCode:Single Number II

    题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...

  4. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  5. [Leetcode] single number ii 找单个数

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

  6. [LeetCode] Single Number II 位运算

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

  7. Leetcode Single Number II (面试题推荐)

    还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here  相信大家都知道用异或在O(n)的时间复 ...

  8. LeetCode | Single Number II【转】

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

  9. LeetCode——Single Number II

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

  10. leetcode Single Number II - 位运算处理数组中的数

    题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...

随机推荐

  1. js 模拟form表单post提交

    var generateHideElement = function (name, value) { var tempInput = document.createElement("inpu ...

  2. [BZOJ1047][HAOI2007]理想的正方形(RMQ+DP)

    题意 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 思路 RMQ求 再DP 代码 #include<cstdio> #i ...

  3. hadoop2-MapReduce详解

    本文是对Hadoop2.2.0版本的MapReduce进行详细讲解.请大家要注意版本,因为Hadoop的不同版本,源码可能是不同的. 以下是本文的大纲: 1.获取源码2.WordCount案例分析3. ...

  4. 封装redis

    封装redis import redis # r = redis.Redis() class MyRedis(): def __init__(self,ip,password,port=6379,db ...

  5. 关于XML

    一.XML定义 XML(eXtensible Markup Language)即可扩展标记语言,它与HTML一样,都是处于SGML,标准通用语言.Xml是Internet环境中跨平台的,依赖于内容的技 ...

  6. 微信小程序开发---自定义组件

    开发者可以将页面内的功能模块抽象成自定义组件,以便在不同的页面中重复使用:也可以将复杂的页面拆分成多个低耦合的模块,有助于代码维护.自定义组件在使用时与基础组件非常相似. 创建自定义组件 类似于页面, ...

  7. WebService常用接口链接(很全面,值得一看)

    天气预报Web服务,数据来源于中国气象局Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmxDisco       ...

  8. 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)

    一.前言 最近整理了一下项目骨架,顺便自定义了一个脚手架,方便日后使用.我会从头开始,步骤一步步写明白,如果还有不清楚的可以评论区留言.先大致介绍一下这个骨架,我们采用 create-react-ap ...

  9. Java作业九(2017-11-6)

    /*圆的类*/ public class R { private double radius; // 构造方法,有参构造 public R(double radius) { this.radius = ...

  10. 【javascript】函数中的this的四种绑定形式 — 大家准备好瓜子,我要讲故事啦~~

       javascript中的this和函数息息相关,所以今天,我就给大家详细地讲述一番:javascript函数中的this   一谈到this,很多让人晕晕乎乎的抽象概念就跑出来了,这里我就只说最 ...