283. 移动零 添加Java版本
This commit is contained in:
parent
e0a6b4050e
commit
79fd10a6c4
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue