알고리즘

[codility] 풀어보기 - BinaryGap

Dohoon 2017. 9. 7. 18:26

알고리즘 공부할 자료를 찾다보니

애증의 codility 에서 연습문제를 주는 것을 발견 !!!!

가능한 하루에 하나씩 풀어보기로 했다.


오늘은 첫 발 시작이 반 

Lesson 1  - BinaryGap

내용 :

 find longest sequence of zeros in binary representation of an integer.

시간 복잡도 : logn


내 코드 : 

function solution(N) {

    // write your code in JavaScript (Node.js 6.4.0)

    let b_str = N.toString(2);

    let gap_pattern = /1[0]+(?=1)/g;

    let zero_pattern = /[0]/g;

    let match_arr =  b_str.match(gap_pattern);

    let max_cnt = 0;

    let now_cnt = 0;

    if(match_arr) {

        for( i in match_arr) {

            now_cnt = match_arr[i].match(zero_pattern).length

            now_cnt > max_cnt ? max_cnt = now_cnt:''  ;

        }

        return max_cnt;

    } 

    else {

        return 0;

    }  

}


소스 공개는 창피하구만.....