먼저, int 범위내의 N을 BinaryString으로 바꾼다.
String에서 제일 처음값은 무조건 1이기 때문에
- 그 이후부터 0이 나올경우 tmpCnt ++
- 1이 나올 경우는 tmpCnt를 result와 비교 후 큰 값으로 result를 바꾼후 tmpCnt 초기화
- 만약 result 값이 length - 1 과 같다면 10000과 같은 경우이므로 0 반환
import java.lang.Integer;
class Solution {
public int solution(int N) {
String tmp = Integer.toBinaryString(N);
int len = tmp.length();
int tmpCnt = 0;
int result = 0;
for(int i = 1; i < (len); i++){
if( tmp.charAt(i) == '0'){
tmpCnt++;
}
else if( tmp.charAt(i) == '1' ){
if(tmpCnt > result){
result = tmpCnt;
}
tmpCnt = 0;
}
}
if( result == (len-1) ){
result = 0;
}
return result;
}
}
'Algorithm' 카테고리의 다른 글
[Codility] PermMissingElem (0) | 2019.05.30 |
---|---|
[Codility] FrogJmp (0) | 2019.05.30 |
[Codility] CyclicRotation (0) | 2019.05.29 |
[Codility] OddOccurencesInArray (0) | 2019.05.28 |
[백준] 11399 ATM (0) | 2019.05.28 |