Skip to content

Commit 1d4efc2

Browse files
author
tonardo2015
committed
add 2 move zero and max consecutive ones
1 parent 02e659f commit 1d4efc2

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

java/283_Move_Zeroes.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public static void moveZeroes(int[] nums){
3+
// index for the zero location to fill
4+
int j = 0;
5+
for(int i = 0; i < nums.length; i++){
6+
if(nums[i] != 0) {
7+
int temp = nums[i];
8+
nums[i] = nums[j];
9+
nums[j] = temp;
10+
j++;
11+
}
12+
}
13+
}
14+
public static void main(String[] args){
15+
int[] nums = {0, 1, 0, 3, 12};
16+
moveZeroes(nums);
17+
for(int num: nums){
18+
System.out.print(num + " ");
19+
}
20+
}
21+
}

java/485_Max_Consecutive_Ones.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class MaxConsecutiveOnes{
2+
public static int findMaxConsecutiveOnes(int[] nums){
3+
int maxConsecutive = 0;
4+
int curCounter = 0;
5+
6+
for(int num: nums){
7+
if(num == 1){
8+
curCounter++;
9+
maxConsecutive = Math.max(maxConsecutive, curCounter);
10+
}
11+
else{
12+
curCounter = 0;
13+
}
14+
}
15+
return maxConsecutive;
16+
}
17+
18+
public static void main(String[] args){
19+
int[] nums = {1, 1, 0, 1, 1, 1};
20+
System.out.println(findMaxConsecutiveOnes(nums));
21+
}
22+
}

0 commit comments

Comments
 (0)