Advertisement
uopspop

Untitled

Sep 10th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package prac;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ReversedBinary {
  6.  
  7.     public static void main(String[] args) {
  8.         int number;
  9.  
  10.         // Scanner in = new Scanner(System.in);
  11.  
  12.         // System.out.println("Enter a positive integer");
  13.         // number = in.nextInt();
  14.         // number = 1041;
  15.         // number = 126;
  16.         // number = 122;
  17.         number = 98;
  18.  
  19.         if (number < 0) {
  20.             System.out.println("Error: Not a positive integer");
  21.         } else {
  22.             ReversedBinary reversedBinary = new ReversedBinary();
  23.             System.out.println(reversedBinary.solution(number));
  24.         }
  25.     }
  26.    
  27.     // O(n)
  28.     public int solution(int N) {
  29.  
  30.         // System.out.print("Convert to binary is:");
  31.         // System.out.print(binaryform(number));
  32.         int maxBinaryGapLen = 0;
  33.        
  34.         /** convert int into binaray string **/
  35.         StringBuilder sb = new StringBuilder();
  36.         convertIntToBinaryString(N, sb); // O(n)
  37.         String numberInBinary = sb.toString();
  38.        
  39.        
  40.         String[] words = numberInBinary.split("");
  41.         int length = words.length;
  42.         int firstOneIndex = -1;
  43.         int secondOneIndex = -1;
  44.         for (int i = 0; i < length; i++) { // O(n)
  45.             if ("1".equals(words[i])) {
  46.  
  47.                 /** 若已經曾經有過一對,則兩個一同往前移動 **/
  48.                 if (firstOneIndex != -1 && secondOneIndex != -1) {
  49.                     firstOneIndex = secondOneIndex;
  50.                     secondOneIndex = i;
  51.                 /** 若為第一個找到的1, 則更新firstOneIndex **/
  52.                 } else if (firstOneIndex == -1) {
  53.                     firstOneIndex = i;
  54.                 /** 若為第二個找到的1, 則更新secondOneIndex **/
  55.                 } else if (firstOneIndex != -1) {
  56.                     secondOneIndex = i;
  57.                 }
  58.  
  59.                 /** 若有secondOneIndex則計算看看 **/
  60.                 if (secondOneIndex != -1) {
  61.                     int currGapLen = getBinaryGapLen(firstOneIndex, secondOneIndex);
  62.                     // System.out.println("firstOneIndex: " + firstOneIndex);
  63.                     // System.out.println("secondOneIndex: " + secondOneIndex);
  64.                     // System.out.println("currGapLen: " + currGapLen);
  65.                     if (currGapLen > maxBinaryGapLen)
  66.                         maxBinaryGapLen = currGapLen;
  67.                 }
  68.             } // end of if ("1".equals(length))
  69.         } // end of for
  70.  
  71. //      System.out.println("maxBinaryGapLen: " + maxBinaryGapLen);
  72.         return maxBinaryGapLen;
  73.     }
  74.  
  75.     private static int getBinaryGapLen(int firstOneIndex, int secondOneIndex) {
  76.         return secondOneIndex - (firstOneIndex + 1);
  77.     }
  78.  
  79.     private static void convertIntToBinaryString(int number, StringBuilder sb) {
  80.         // System.out.println("number: " + number);
  81.  
  82.         // 取最後一個binary的餘數
  83.         int remainder = number % 2;
  84.         // System.out.println("remainder: "+ remainder);
  85.  
  86.         // 將餘數放到最前面
  87.         sb.insert(0, remainder);
  88.  
  89.         // 終止條件
  90.         if (number <= 1) {
  91.             return; // KICK OUT OF THE RECURSION
  92.         }
  93.  
  94.         int number2 = number >> 1; // 全部往右移一格
  95.         convertIntToBinaryString(number2, sb);
  96.         // sb.append(remainder);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement