Binary Gap(二進制空白)

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

英文描述

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.git

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.github

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.spa

Write an efficient algorithm for the following assumptions:翻譯

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

中文描述

這裏我不按照原文一字一字的翻譯,可是儘可能按照題目的要求把題目解釋清楚。ci

這裏題目的要求是,將 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

相關文章
相關標籤/搜索