1. 程式人生 > >Binary Gap(二進位制空白)

Binary Gap(二進位制空白)

中文標題【二進位制空白】

英文描述

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Write a function:

class Solution { public int solution(int N); }

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..2,147,483,647].

中文描述

這裡我不按照原文一字一字的翻譯,但是儘量按照題目的要求把題目解釋清楚。

這裡題目的要求是,將 N 為一個整數型別的資料,轉換為一個 2 進位制的字串,然後在返回的字串中返回最大的 0 的間隔數字。

例如 529 轉換為 2 進位制的字串為:1000010001,在這裡,將會存在以 1 為分割的字串  0000 和 000,這 2 個字串的長度分別為 4 和 3。

我們的演算法需要返回的值誒 4。

思路和點評

這個題目的思路其實比較簡單,你需要首先將 N 這個整數,轉換為 0 和 1 的字串。然後在轉換成功的字串中返回以 1 分分割的 0 的長度。

這裡可能需要考慮下面的幾種情況。

情況 結果
11 這個情況應該返回的長度為 0
10 這個情況因為沒有被 1 這個字串封閉,因此應該返回長度為 0

傳統的思路應該是採取字串分割的方式,進行遍歷後獲得結果。

我們在這裡採取一種相對不是非常常規的方式,例如在 10000010001 字串中插入 #,將字串變為 #1#00000#1#000#1#。

然後將字串按照 1 進行分割,那麼分割後的陣列應該分別儲存的資料為:#,#0000#,#000#,#

這裡我們只需要找到 #...# 中值最大的連續 0 字串就可以了。基本上可以使用 1 個字串替換函式和一個字串分割函式就可以了,並不需要多次儲存和遍歷。

原始碼

原始碼和有關程式碼的更新請訪問 GitHub:

https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/codility/CodilityBinaryGapTest.java

程式碼思路請參考:

package com.ossez.lang.tutorial.tests.codility;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * More details about question see link below
 * <ul>
 * <li>@see <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap</a>
 * </li>
 * </ul>
 * </p>
 * 
 * @author YuCheng
 *
 */
public class CodilityBinaryGapTest {

  private final static Logger logger = LoggerFactory.getLogger(CodilityBinaryGapTest.class);

  /**
   * 
   */
  @Test
  public void testMain() {
    logger.debug("BEGIN");

    int N = 529;
    String intStr = Integer.toBinaryString(N);

    intStr = intStr.replace("1", "#1#");

    String[] strArray = intStr.split("1");

    int maxCount = 0;
    for (int i = 0; i < strArray.length; i++) {
      String checkStr = strArray[i];
      int countLength = 0;

      if (checkStr.length() > 2 && checkStr.startsWith("#") && checkStr.endsWith("#")) {
        checkStr = checkStr.replace("#", "");
        countLength = checkStr.length();

        if (maxCount < countLength) {
          maxCount = countLength;
        }

      }
    }

    logger.debug("MAX COUNT: [{}]", maxCount);
  }

}

https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap