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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章