알고리즘
알고리즘/10진수를 2진수로 변환하여 연속되는 0의 길이 가장 긴 값_PHP
개발쟝
2018. 5. 15. 01:00
function solution($N) { $max = 0; $cnt = array(); $bin_num = array(); $re_bin_num = array(); if ($N > 0) { while ( $N > 1) { $bin_num[] = $N % 2; $N = $N / 2; } } if (sizeof($bin_num) > 0) { $size = sizeof($bin_num)-1; $idx = 0; for ($i = $size ; $i >= 0 ; $i--) { if ($bin_num[$i]) { if (sizeof($cnt) > 0 && $cnt[$idx] > 0) { $max = $max < $cnt[$idx] ? $cnt[$idx] : $max; $idx++; } $cnt[$idx] = 0; } else $cnt[$idx]++; } } return $max; }