Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 397f064

Browse files
solves move zeros
1 parent e23aa36 commit 397f064

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
|------|:--------:|:----------------------:|
4242
| [Replace Elements with Greatest Element on Right Side](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3259/) | [![Java](assets/java.png)](java/src/ReplaceElementsWithGreatestOnTheRightSide.java) [![Python](assets/python.png)](python/replace_elements_with_greatest_on_right.py) |
4343
| [Remove Duplicates from Sorted Array](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3258/) | [![Java](assets/java.png)](java/src/RemoveDuplicatesFromSortedArray.java) [![Python](assets/python.png)](python/remove_duplicate_elements_from_sorted_array.py) |
44-
| [Move Zeros](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3157/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
44+
| [Move Zeros](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3157/) | [![Java](assets/java.png)](java/src/MoveZeros.java) [![Python](assets/python.png)](python/move_zeros.py) |
4545
| [Sort Array By Parity](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3260/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
4646
| [Remove Element](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3575/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
4747

‎java/src/MoveZeros.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class MoveZeros {
2+
public void moveZeroes(int[] array) {
3+
int j = 0;
4+
for (int i = 0 ; i < array.length ; i++) {
5+
if (array[i] != 0) {
6+
array[j++] = array[i];
7+
}
8+
}
9+
while (j < array.length) {
10+
array[j++] = 0;
11+
}
12+
}
13+
}

‎python/move_zeros.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def moveZeroes(self, nums: List[int]) -> None:
6+
finalLength = 0
7+
index = 0
8+
while index < len(nums):
9+
if nums[index] != 0:
10+
nums[finalLength] = nums[index]
11+
finalLength += 1
12+
index += 1
13+
while finalLength < len(nums):
14+
nums[finalLength] = 0
15+
finalLength += 1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /