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. Servlet上

    JavaWeb应用的概念 Java Web应用由一组Servlet.HTML页.类.以及其它可以被绑定的资源构成.它可以在各种供应商提供的实现Servlet规范的 Servlet容器 中运行. Jav ...

  2. ubuntu 添加源

    edit file :  /etc/apt/sources.list add: deb http://mirrors.163.com/ubuntu/ intrepid main restricted ...

  3. 百度Paddle会和Python一样,成为最流行的深度学习引擎吗?

    PaddlePaddle会和Python一样流行吗? 深度学习引擎最近经历了开源热.2013年Caffe开源,很快成为了深度学习在图像处理中的主要框架,但那时候的开源框架还不多.随着越来越多的开发者开 ...

  4. 《foreach循环示例》

    //foreach测试 public class ForEachTest { public static void main(String[] args) { String[] books = {&q ...

  5. catch的执行与try的匹配

    这里有一段代码: public class EmbededFinally { public static void main(String args[]) { int result; try { Sy ...

  6. hdu 2052

    PS:竟然一次AC....惊喜...注意每个实例后跟一个空行.. 学到一个快速清空数组的用法...memst函数.  memst(void *s,char a,sizeof n)  把 s里面的前n个 ...

  7. bistu新生-1005

    #include "stdio.h"#include "string.h"int main(){ char ku[]={'0','1','2','3','4', ...

  8. 简单实现web单点登录

    主要参考文档:http://blog.csdn.net/jimmy609/article/details/18605781 1.工程总体结构: 2.修改C:\Windows\System32\driv ...

  9. shell脚本入门教程(转)

    http://bbs.chinaunix.net/thread-391751-1-1.html http://www.cnblogs.com/suyang/archive/2008/05/18/120 ...

  10. python基础之模块之os模块

    os模块 os模块的作用: os,语义为操作系统,所以肯定就是操作系统相关的功能了,可以处理文件和目录这些我们日常手动需要做的操作,就比如说:显示当前目录下所有文件/删除某个文件/获取文件大小…… 另 ...