Skip to content

Commit b8d68bf

Browse files
committed
연속된 바이너리값 찾기
1 parent d01cdd7 commit b8d68bf

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

MaxConsecutive.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class MaxConsecutive {
2+
public static void main(String[] args) {
3+
int[] nums = {1, 1, 1, 1, 1, 0, 1};
4+
System.out.println(findMaxConsecutiveOnes(nums));
5+
}
6+
7+
//solution 1
8+
public static int findMaxConsecutiveOnes(int[] nums) {
9+
int cnt = 0, max = 0;
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
if (nums[i] == 1) {
13+
cnt++;
14+
if (i == nums.length - 1 && nums[nums.length - 1] == 1) {
15+
if (cnt > max) return cnt;
16+
else return max;
17+
}
18+
} else if (cnt > max) {
19+
max = cnt;
20+
cnt = 0;
21+
} else
22+
cnt = 0;
23+
}
24+
return max;
25+
}
26+
//solution 2
27+
/*
28+
public static int findMaxConsecutiveOnes(int[] nums) {
29+
int max = 0;
30+
int cnt = 0;
31+
32+
for (int i = 0; i < nums.length; i++) {
33+
if (nums[i] == 1)
34+
cnt++;
35+
else
36+
cnt = 0;
37+
38+
if (cnt > max)
39+
max = cnt;
40+
}
41+
42+
return max;
43+
44+
}*/
45+
}

0 commit comments

Comments
 (0)