-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution283.java
More file actions
64 lines (60 loc) · 1.34 KB
/
Copy pathSolution283.java
File metadata and controls
64 lines (60 loc) · 1.34 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @Title: Solution283.java——
* @Package EasyCode
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月14日 下午4:13:50
* @version V1.0
*/
package EasyCode;
import java.util.Vector;
/**
* @ClassName: Solution283——Move Zeros
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月14日 下午4:13:50
*/
public class Solution283 {
//时间空间复杂度均为O(n)级别
/* public static void moveZeros(int[] nums){
Vector<Integer> newnums = new Vector<>();
for(int i = 0 ; i < nums.length ; i++){
if(nums[i] != 0)
newnums.add(nums[i]);
}
for(int i = 0 ; i < newnums.size() ; i ++)
nums[i] = newnums.get(i);
for(int i = newnums.size() ; i < nums.length ; i ++)
nums[i] = 0;
}*/
//原地执行算法,不需要申请额外的空间
/* public static void moveZeros(int[] nums){
int r = 0 ;
for(int i = 0 ; i < nums.length ; i++){
if(nums[i] != 0){
nums[r] = nums[i];
r++;
}
}
for(int i = r ; i < nums.length - 1 ; i ++)
nums[i] = 0;
}*/
//再次优化,减少多重循环
public static void moveZeros(int[] nums){
int r = 0 ;
for(int i = 0 ; i < nums.length ; i++){
if(nums[i] != 0){
int tmp = nums[r];
nums[r] = nums[i];
nums[i] = tmp;
r++;
}
}
}
public static void main(String[] args) {
int[] arr = {1,5,0,34,0,2,3,7,0};
moveZeros(arr);
for(int i : arr)
System.out.print(i);
}
}