BinaryGap Test for codility

问题的原文请参考链接:
https://app.codility.com/programmers/lessons/1-iterations/binary_gap/

有关问题的解答思路和翻译,请参考:https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap
This commit is contained in:
YuCheng Hu 2018-12-23 16:30:53 -05:00
parent 3c98c4008d
commit c6eff656c0
1 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,56 @@
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);
}
}