137.Single Number II

来源:互联网 发布:php取cookie取出失败 编辑:程序博客网 时间:2024/06/11 13:38

题目链接:single-number-ii


import java.util.Arrays;/** *  * Given an array of integers, every element appears three 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 SingleNumberII {//11 / 11 test cases passed.//Status: Accepted//Runtime: 285 ms//Submitted: 0 minutes ago    static int singleNumber(int[] A) {    int N = 32;    int[] count = new int[N];    int result = 0;    Arrays.fill(count, 0);    for (int i = 0; i < A.length; i++) for (int j = 0; j < count.length; j++) count[j] += ((A[i] >> j) & 1);    for(int i = 0; i < N; i ++)     result += ((count[i] % 3) << i);    return result;    }public static void main(String[] args) {System.out.println(singleNumber(new int[] {1}));}}


0 0
原创粉丝点击