forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution2.java
35 lines (27 loc) · 876 Bytes
/
Solution2.java
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
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
import java.util.*;
// Time Complexity: O(n)
// Space Complexity: O(n)
class Solution2 {
public void moveZeroes(int[] nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.length ; i ++)
if(nums[i] != 0)
nums[k++] = nums[i];
// make the nums[k...end) zeros
for(int i = k ; i < nums.length ; i ++)
nums[i] = 0;
}
private static void printArr(int[] arr){
for(int i = 0 ; i < arr.length ; i ++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]){
int[] arr = {0, 1, 0, 3, 12};
(new Solution2()).moveZeroes(arr);
printArr(arr);
}
}