Leetcode 137. Single number II Java language

Source: Internet
Author: User

Given an array of integers, every element appears three times except for one, which appears exactly once. Find the single one. Note:your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?

Test instructions: In an array except for one number, the other number appears three times!! Find the number that appears once!!!

Public class solution {    public int singlenumber (int[]  Nums)  {        int ret=0;         int[] count=new int[32];        int  Len=nums.length;        for (int i=0;i<32;i++) {             for (int j=0;j<len;j++) {             //statistics All numbers of the I-bit 0,1 and. Get!!!!!! by right shift with 1 doing & operations                 count[i]+= (( Nums[j]>>i) &1);              //// And then take the bit and modulo 3 to get the single number of the first I bit OH                  count[i]=count[i]%3;            }             ////because I moved to the left I bit, so now move right back, then with 0 do or | arithmetic              ////, all 1 can be attributed to!!!!!. My Cao is really 6666            ret |= (count[i]<<i) ;        }        return  ret;    }}

PS: Originally looked at the left teacher's book, originally thought can use K into the system without carry addition to do again. See the left teacher's book for details. But I don't know why I can't pass. So on the Internet Ba la a more general method.

Input is an int array, so you can use 32-bit to express the elements of the input array.
Assuming that there is not a single number in the input, then each digit in the input repeats the number, that is, for each of the 32 bits I, all inputs are added up, and the I bit must be a multiple of 3.
Now that you have added a single number, do the same for each of the 32 bits, that is, place all the inputs together, and take a look at the remainder of the bit I and divided by 3, which is the value of the single numer in the first bit. This gives the value of the single number in the first bit. This is equivalent to a simulated binary, and then simply converts the analog binary into a decimal output.
In order to complete the above procedure, you need to use an array of int a[32] to simulate the bitwise operation.
It can be seen that this approach to the requirements of the very high, need to see the number of times, the present is the digital behind the binary composition, otherwise it is difficult to think of a bit to deal with.
In addition, this practice can be extended, if there is a bunch of input, 1 of the numbers appear 1 times, the remaining number is K, so the problem can be used in this way.

Public class solution {    public int singlenumber (int[]  Nums)  {        //if (nums==null | | &NBSP;NUMS.LENGTH&LT;4) return 0;        int[] ret=new  Int[32];        for (int i=0;i!=nums.length;i++) {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SUM3 (Ret,nums[i]);      &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;GAIN101 (ret);             }    public static void &NBSP;SUM3 (int[] ret,int nums) {        // int[]  Ret=new int[32];        // for (int i=0;i<nums.length ; i++) {        //   int[] cur=to3 (Nums[i]);        //      for (int j=0;j<cur.length;j++) {                  ret[j]= (Ret[j]+cur[j])%3;         //      }           // }        int[] cur=to3 (nums);         for (int i=0;i!=ret.length;i++) {             ret[i]= (Ret[i]+cur[i])%3;         }        //return ret;    }     public static int[] to3 (Int num) {         int[] ret=new int[32];        int index=0;         while (num!=0) {            ret[index++]=num%3 ;            num=num/3;         }        return ret;     }    // public static int gain10 (Int[] arr) {     //     int ret=0;    //      int temp=0;    //     for (int i=0;i<arr.length ; i++) {    //         temp= (int) Math.pow (3,i);     //         ret=ret+arr[i]*temp;     //     }    //     return ret;     // }    public static int gain101 (Int[] arr) {         int ret=0;         for (int i=arr.length-1;i!=-1;i--) {             ret=ret*3+arr[i];        }         return ret;    }}


Leetcode 137. Single number II Java language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.