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. 从协议VersionedProtocol开始

    VersionedProtocol协议是Hadoop的最顶层协议接口的抽象:5--3--3共11个协议,嘿嘿 1)HDFS相关 ClientDatanodeProtocol:client与datano ...

  2. [转][C/C++]函数名字修饰(Decorated Name)方式

    1.C/C++函数修饰名: 对于我们的C/C++源程序而言,函数名只是函数的一小部分,函数还有调用方式(参数入栈方式).返回值类型.参数个数和各参数类型等信息,对于C++类成员函数,还有更多信息.这些 ...

  3. joinfetch之意义

    既然被join的对象早晚都要用到,为什么要先从A表取这边的独享,再根据关联关系取B表中的对象,分两次或者多次进行,增加数据库的负载呢? 为什么不把A表和B表join成一张表,从这个组合表中把要取的对象 ...

  4. IOS UTF8中文字母数字 组合时长度截取

    //计算总共字数和限制字数的Index位置 -(NSMutableArray *) unicodeLengthOfString: (NSString *) text { NSMutableArray ...

  5. 提交自己的插件包(package)

    安装 把下列命令粘贴到终端上: mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins; curl -L htt ...

  6. Chapter 5: Container

    Chapter 5: Container A container is a module that processes the requests for a servlet and populates ...

  7. KochSnow曲线

    在这里实现了Koch曲线,而且提到我们只需要对一个等边三角形的各条边按照Koch曲线的算法进行绘图就能得到KochSnow曲线,将其实现到之前提到的绘图框架中,考虑到KochSnow的实现主要依赖Ko ...

  8. Server.Transfer和Response.Redirect区别

    根本上,Response是叫浏览器去重新转向到指定的网页,而Server自然是发生在服务器端为主了,因此会有以下区别:1. Server.Transfer只能够转跳到本地虚拟目录指定的页面,而Resp ...

  9. C#泛型(二)

    <1>.泛型方法 以前文章说到用一个泛型类 SortHelper 来做一个冒泡排序的处理,下面回顾一下之前的代码: public class SortHelper<T> whe ...

  10. iOS System Services

    System Services is a singleton class to gather all available information about a device. Over 75 met ...