-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove_Zeroes.java
More file actions
35 lines (32 loc) · 880 Bytes
/
Move_Zeroes.java
File metadata and controls
35 lines (32 loc) · 880 Bytes
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
package com.leet_code;
import java.util.Arrays;
public class Move_Zeroes {
public static void main(String[] args) {
int[] arr={0,1,0,3,12};//[1,3,12,0,0]
moveZeroes(arr);
System.out.println(Arrays.toString(arr));
}
public static void moveZeroes(int[] nums) {
int left = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] != 0) {
int temp = nums[right];
nums[right] = nums[left];
nums[left] = temp;
left++;
}
}
}
public void moveZeroes2(int[] nums) {/// musta approch
int j=0;
for( int num :nums){
if(num != 0){
nums[j]=num;
j++;
}
}
for( int i=j;i<nums.length;i++){
nums[i]=0;
}
}
}