283. 移动零 添加Java版本

This commit is contained in:
posper
2021-07-28 14:13:51 +08:00
parent e0a6b4050e
commit 79fd10a6c4

View File

@ -64,6 +64,21 @@ public:
Java Java
```java
public void moveZeroes(int[] nums) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
if (nums[fast] != 0) {
nums[slow++] = nums[fast];
}
}
// 后面的元素全变成 0
for (int j = slow; j < nums.length; j++) {
nums[j] = 0;
}
}
```
Python Python
```python ```python