유베와서 예전에 풀어봤던 코딜리티 문제를 풀어 보기로함
그뗀 80%였는데

오늘 처음 풀었을땐 40% ,
그 다음은 80%,
마지막 100%가 나와서 기분이 좋다 비록 1단계이지만,

통과하지 못한 데이터를 알려줘서 더 쉽게 문제점을 찾을 수 있었던거 같다.

실제로 알고리즘 문제를 풀면서 내가 예상하지 못한 케이스에서 틀릴 가능성이 커 무섭다

문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BinaryGap

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2.
The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3.
The number 20 has binary representation 10100 and contains one binary gap of length 1.
The number 15 has binary representation 1111 and has no binary gaps.
The number 32 has binary representation 100000 and has no binary gaps.

Write a function:

class Solution { public int solution(int N); }

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..2,147,483,647].
Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
테스트 코드
@Test
// number 9 has binary representation 1001 and contains a binary gap of length 2.
public void testCase1() {
assertEquals(2, binaryGap.solution(9));
}

@Test
// The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3.
public void testCase2() {
assertEquals(4, binaryGap.solution(529));
}

@Test
// The number 20 has binary representation 10100 and contains one binary gap of length 1.
public void testCase3() {
assertEquals(1, binaryGap.solution(20));
}

@Test
// The number 15 has binary representation 1111 and has no binary gaps.
public void testCase4() {
assertEquals(0, binaryGap.solution(15));
}

@Test
// The number 32 has binary representation 100000 and has no binary gaps.
public void testCase5() {
assertEquals(0, binaryGap.solution(32));
}

@Test
// given N = 1041 the function should return 5.
public void testCase6() {
assertEquals(5, binaryGap.solution(1041));
}

@Test
// given N = 561892 the function should return 3.
public void testCase7() {
assertEquals(3, binaryGap.solution(561892));
}

@Test
// given N = 74901729 the function should return 4.
public void testCase8() {
assertEquals(4, binaryGap.solution(74901729));
}

@Test
// given N = 1376796946 the function should return 5.
public void testCase9() {
assertEquals(5, binaryGap.solution(1376796946));
}

100% 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public int solution(int N) {
int answer = 0;
int zeroCount = 0;
boolean bouder = false;
for (; N > 0; ) {
if (N % 2 == 1) {
bouder = true;
if (answer <= zeroCount) {
answer = zeroCount;
zeroCount = 0;
} else zeroCount = 0;
} else if (bouder && N % 2 == 0) {
zeroCount += 1;
}
N = N / 2;
}
return answer;
}