상세 컨텐츠

본문 제목

[Codility]BinaryGap

Development/알고리즘

by J-Developer 2020. 6. 27. 16:52

본문

반응형

[문제]

BinaryGap

[답안 작성]

class Solution {
    public int solution(int N) {
		

        String binaryString = Integer.toBinaryString(1073741825);
		       
        char[] binaryChar = binaryString.toCharArray();
        
        boolean firstOne = false;
        int result = 0;
        int tempNum = 0;
        
        for( char c : binaryChar ) {
            
            if( c == '1' && firstOne && tempNum == 0 ) {
                firstOne = false;
            }
            
            if( c == '1' && !firstOne ) {
                
                firstOne = true;
                
            }
            else if( c == '1' && firstOne ) {
                
                if( result < tempNum ) {
                    result = tempNum;
                }
                
                tempNum = 0;
                
            }
            else if( firstOne && c == '0' ){
                
                ++tempNum;
                
            }
            
        }
        
        return result;
    }
}
반응형

'Development > 알고리즘' 카테고리의 다른 글

[Codility] TapeEquilibrium  (0) 2020.06.28
[Codility]PermMissingElem  (0) 2020.06.28
[Codility]FrogJmp  (0) 2020.06.27
[Codiliy]OddOccurrencesInArray  (0) 2020.06.27
[Codility]CyclicRotation  (0) 2020.06.27

관련글 더보기

댓글 영역