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?

求出数组中只出现一次的数,剩下的都是三次。

/**
* Created by wangzunwen on 2016/11/14.
*/
public class singleNumber2 { public int singleNumber(int[] nums) { int result = 0; for( int i = 0;i<32;i++){ int num = 0;
for( int j = 0;j<nums.length;j++){
if(( nums[j] >> i &1) == 1 ){
num++;
} }
num%=3;
result+=(num<<i);
}
return result; }
}
public class Solution {
public int singleNumber(int[] A) {
int n = A.length;
int one=0, two=0, three=0;
for(int i=0; i<n; i++){
two |= one&A[i];
one^=A[i];
//cout<<one<<endl;
three=one&two;
one&= ~three;
two&= ~three;
}
return one; }
}

leetcode 137. Single Number II ----- java的更多相关文章

  1. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  2. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  3. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  4. LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

    翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...

  5. [LeetCode] 137. Single Number II 单独的数字之二

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  6. 详解LeetCode 137. Single Number II

    Given an array of integers, every element appears three times except for one, which appears exactly ...

  7. Java for LeetCode 137 Single Number II

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

  8. Java [Leetcode 137]Single Number II

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

  9. LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数

    Given an array of integers, every element appears three times except for one, which appears exactly ...

随机推荐

  1. struts中的数据校验

    1.struts中如何进行数据校验 在每一个Action类中,数据校验一般都写在业务方法中,比如login().register()等.struts提供了数据校验功能.每个继承自ActionSuppo ...

  2. java基础之hashmap

    Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多,但到底是自己学习的总结,就发出来跟大家一起分享,一起讨论. 1.hashma ...

  3. Unity安卓连接profile调试

    通过USB ADB 1.从Unity中Export Android 工程的时候一定要勾选 Development Build,autoconnect profiler 2.cmd进入adb的目录(打开 ...

  4. HTML--8Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:     var a =docunme ...

  5. HTML--4格式布局

    一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例: 二.position:absolute 1.外层没有position:absolute(或r ...

  6. HashMap简单理解

    1. hashmap基于哈希表的map接口实现,此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable ...

  7. angularjs 选项卡 --- 自定义属性

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  8. list.clear()和list=null的区别

    以前并没有注意到list.clear()和list=null的区别,其实,区别在于 clear()方法是将list清空,但是对象的引用还在,只不过是一个表现为空引用 list=null是将list对象 ...

  9. hdu 2085

    PS:递推题..  a[n]=a[n-1]*3+2*b[n-1]  b[n]=a[n-1]+b[n-1] 代码: #include "stdio.h" ]; ]; int main ...

  10. Qemu文档

    http://wiki.qemu.org/Manual http://qemu.weilnetz.de/qemu-doc.html http://www.linuxcertif.com/man/1/q ...