forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution3.java
37 lines (29 loc) · 890 Bytes
/
Solution3.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
36
37
/// 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 Solution3 {
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)
swap(nums, k++, i);
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
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 Solution3()).moveZeroes(arr);
printArr(arr);
}
}