forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveZeroes.java
More file actions
38 lines (34 loc) · 802 Bytes
/
MoveZeroes.java
File metadata and controls
38 lines (34 loc) · 802 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
36
37
38
package normal; /**
* @program JavaBooks
* @description: 283.移动零
* @author: mf
* @create: 2019/11/04 18:29
*/
import java.util.Arrays;
/**
* 题目:https://leetcode-cn.com/problems/move-zeroes/
* 难度:easy
* 类型:数组
*/
/*
输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
*/
public class MoveZeroes {
public static void main(String[] args) {
int[] nums = {0,1,0,3,12};
moveZeroes(nums);
System.out.println(Arrays.toString(nums));
}
private static void moveZeroes(int[] nums) {
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[index++] = nums[i];
}
}
for (int i = index; i < nums.length; i++) {
nums[i] = 0;
}
}
}