Three questions 2015.8.26

Source: Internet
Author: User

1: Number of 1 in binary

The first approach: the most straightforward is to divide the number of pairs into two and the remainder operation. The number of 1 in the remainder of the statistic

The second approach: the binary number and 1 are done with the operation, and then move to the right.

But one problem here is the right-shift operator, which discards the rightmost few. And easy to fall into the cycle of death, the sword refers to the offer on page 79th and gives an explanation.

If the number is an unsigned number, the leftmost n bit is filled with 0, and if the number is a signed number, the number's sign bit fills the leftmost n bit.

The third approach is also my best practice: To do and operate the integer with the number minus 1.

int c = 10; System.out.println (Integer.tobinarystring (c));

  

2: Given an array, know that there is a number in the array that occurs more than half of the total, to find out the number

The median of the sorted array must be this number, and if it is completely sorted, it will take a lot of time. For fast sequencing, each trip can determine the final position of an element.

The first algorithm for fast sorting must be mastered: The following is the code:

Determine the position of the sort element public static int Identitytheindex (int[] a,int low,int high) {int key = A[low];while (Low

Inspired by the quick sort, can be judged by the sort side, the code is as follows:

public static int Morethanhalfnumber (int[] a,int length) {int mid = length >> 1;int high = length-1;int Low = 0;int index = Identifytheindex (A,low,high), while (Index!=mid) {if (index>mid) {high = Index-1;index = Identifytheindex (A, Low,high);} Else{low = index + 1;index =identifytheindex (A,low,high);}} int result = A[mid];return result;}

The second method: consider traversing the array when you save two values: One is an array of numbers, one is the number of times, when traversing the next number, if the next number is the same as the previous saved number, the number of times plus 1, if the next number and the previous saved number is different, the number of times minus 1, if the number is 0, You need to save the next number and set the number of times to 1. Since the number to be found is more than the number of occurrences of all other numbers, the number to be searched is definitely the last number to be set to 1 o'clock. The code is as follows:

public static int MoreThanHalfNumber2 (int[] a,int length) {int result = A[0];int time = 1;for (int  i = 1;i<length;i++ {if (time = = 1) {result = A[i];time = 1;} else if (result = = A[i]) {time + +;} Else{time--;}} return result;}

3: Minimum (large) number of K

The problem can be based on the idea of the main determination of the position of the K element, the most direct idea is to quickly sort.

The second method is particularly suitable for handling massive amounts of data, first creating a data container of size k to store the smallest number of k, and then reading a number from the n number of input each time, if there are fewer than k in the container, then read directly, if there are already k, that is, the container is full, then can only be replaced. The first is to find out the maximum number of k numbers, if the next number is less than the maximum number will be replaced, otherwise continue to traverse the next number

Therefore, there are 3 things to do when the container is full:

1) Find the largest number in K integers

2) It is possible to delete the maximum number in this container

3) It is possible to insert a new number

If a binary tree is used to implement this data container, it can be completed in O (LOGK) time, and for n input numbers, the total time efficiency is O (NLOGK)

It's easy to think of the biggest heap.

Let's review the process of building the heap and sorting the heap.

Build heap: From the bottom up, build the heap, the root node maximum or minimum. The code is as follows:

public static void Createmaxdheap (int[] data, int. LastIndex) {for (int i = (lastIndex-1)/2; I >= 0; i--)              {//Save the node currently being judged int k = i;                  If the child node of the current node exists while (2 * k + 1 <= lastIndex) {///Biggerindex always records the value of the larger node, first assigns the value to the left child node of the current judging node                  int Biggerindex = 2 * k + 1; if (Biggerindex < LastIndex) {//If the right child node is present, the biggerindex should be equal to LastIndex if (d                          Ata[biggerindex] < Data[biggerindex + 1]) {//If the right child node value is greater than the value of the left Dial hand node, then Biggerindex records the value of the right child node                      biggerindex++; }} if (Data[k] < Data[biggerindex]) {//If the current node value is smaller than the maximum value of the child node, then the value of 2 is exchanged                      , Biggerindex value is assigned to K Swap (data, K, Biggerindex) after Exchange;                  K = Biggerindex;                  } else {break;  }              }          }    }     

Heap sort: Outputs the built-in heap root node, then swaps it with the last leaf node and then re-builds the heap, at which point the number of elements to build the heap is less.

The code is as follows:

public static void Heapsort (int[] data) {          for (int i = 0; i < data.length; i++) {              createmaxdheap (data, Data.len Gth-1-i);              Swap (data, 0, data.length-1-i);              print (data);          }      }  public static void Swap (int[] data, int i, int j) {          if (i = = j) {              return;          }          Data[i] = Data[i] + data[j];          DATA[J] = Data[i]-data[j];          Data[i] = Data[i]-data[j];      }  

And this problem is only required to find the smallest number of K is not ordered, so you can build the operation of the heap, the code is as follows:

public static void Createmaxdheap (int[] data, int. LastIndex) {for (int i = (lastIndex-1)/2; I >= 0; i--)              {//Save the node currently being judged int k = i;                  If the child node of the current node exists while (2 * k + 1 <= lastIndex) {///Biggerindex always records the value of the larger node, first assigns the value to the left child node of the current judging node                  int Biggerindex = 2 * k + 1; if (Biggerindex < LastIndex) {//If the right child node is present, the biggerindex should be equal to LastIndex if (d                          Ata[biggerindex] < Data[biggerindex + 1]) {//If the right child node value is greater than the value of the left Dial hand node, then Biggerindex records the value of the right child node                      biggerindex++; }} if (Data[k] < Data[biggerindex]) {//If the current node value is smaller than the maximum value of the child node, then the value of 2 is exchanged                      , Biggerindex value is assigned to K Swap (data, K, Biggerindex) after Exchange;                  K = Biggerindex;                  } else {break;  }              }          }    public static void print (int[] data) {for (int i = 0; i < data.length; i++) {System.out.          Print (Data[i] + "\ t");      } System.out.println (); } public static int[] Getfirstnumber (int[] A, int k) {int[] B = new Int[k];for (int i =0;i<k;i++) {b[i] = a[i];} Heapsort (B); int count = K;while (count!=a.length) {/*int Minmumvalue = b[0];int Maxmumvalue = B[k-1];*/int MaxmumValue = B[0 ];//system.out.println ("The smallest element after sorting:" +minmumvalue); if (Maxmumvalue>a[count]) {int temp = A[count]; B[0] = temp;} System.out.println ("B after replacing element:");p rint (b); System.out.println ("..."); Heapsort (B); count++;}, the ". ..... ..... ............."; return B;} public static void Main (string[] args) {int[] A = {80,55,45,7,8,6,3,10,2,1};int k = 4;int[] C = Getfirstnumber (a,k); System.out.print ("The Xiao K number is:"), for (int i = 0;i<k;i++) {System.out.print (c[i] + "\ t");}} The output of the program is: 8055457805545780554578055457 replace the element after the b:855457 ..... ..... ................. 558457558457558457558457 after replacing the elementb:68457 ....... ....................... 45867458674586745867 Replace the element after the b:3867 ..... ....... ................. 8763876387638763 Replace the element after the b:8763 ..... ....... ................. 8763876387638763 Replace the element after the b:2763 ..... ....... ................. 7362736273627362 Replace the element after the b:1362 ..... ....... ................. 6312631263126312 The Xiao K number is: 6312

  

Three questions 2015.8.26

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.