Skip to content

Commit ee9ce6a

Browse files
committed
283. Move Zeroes
- Language: Python. - Time complexity: O(n). - Space complexity: O(1).
1 parent 00282eb commit ee9ce6a

File tree

4 files changed

+90
-35
lines changed

4 files changed

+90
-35
lines changed

python_solutions/lc_283_move_zeroes/__init__.py

Whitespace-only changes.

python_solutions/lc_283_move_zeroes/lc_283_move_zeroes.py renamed to python_solutions/lc_283_move_zeroes/lc_283.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ def moveZeroesV1(self, nums: list[int]) -> None:
2525
i += 1
2626
for i, num in enumerate(non_zero_nums):
2727
nums[i] = 0 if num is None else num
28+
29+
def moveZeroesV2(self, nums: list[int]) -> None:
30+
# Time complexity: O(n). Space complexity: O(1).
31+
i, j = 0, 1
32+
while j < len(nums):
33+
if nums[i] == 0 and nums[j] != 0:
34+
nums[i], nums[j] = nums[j], nums[i]
35+
i += 1
36+
elif nums[i] != 0:
37+
i += 1
38+
j += 1
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import pytest
2+
3+
from lc_283_move_zeroes.lc_283 import Solution
4+
5+
test_cases = (
6+
# PRESET CASES
7+
pytest.param([0, 1, 0, 3, 12], [1, 3, 12, 0, 0], id="Preset case 1"),
8+
pytest.param([0], [0], id="Preset case 2"),
9+
# COMMON CASES
10+
# все ноли в конце. массив не меняется.
11+
pytest.param([1, 3, 12, 0, 0], [1, 3, 12, 0, 0], id="Trailing zeros"),
12+
# все ноли в начале
13+
pytest.param([0, 0, 0, 1, 2, 3], [1, 2, 3, 0, 0, 0], id="Leading zeros"),
14+
# ноли в середине. порядок ненулевых элементов
15+
pytest.param([1, 0, 2, 0, 3, 0], [1, 2, 3, 0, 0, 0], id="Embedded zeros"),
16+
# чередующиеся ноли и не ноли. обработка чередующегося паттерна - старт с ноля.
17+
pytest.param(
18+
[0, 1, 0, 2, 0, 3, 0, 4],
19+
[1, 2, 3, 4, 0, 0, 0, 0],
20+
id="Alternating zeros and non-zeros (zero start)",
21+
),
22+
# чередующиеся ноли и не ноли. обработка чередующегося паттерна - старт с не ноля.
23+
pytest.param(
24+
[1, 0, 2, 0, 3, 0, 4],
25+
[1, 2, 3, 4, 0, 0, 0],
26+
id="Alternating zeros and non-zeros (non-zero start)",
27+
),
28+
# ноли в начале. ноли в конце.
29+
pytest.param(
30+
[0, 0, 0, 1, 3, 12, 0, 0],
31+
[1, 3, 12, 0, 0, 0, 0, 0],
32+
id="Leading and trailing zeros",
33+
),
34+
# большие числа
35+
pytest.param(
36+
[1000000, 0, 2**31, 0, 0, 42],
37+
[1000000, 2**31, 42, 0, 0, 0],
38+
id="Large numbers",
39+
),
40+
# CORNER CASES
41+
# нет нолей. массив не меняется.
42+
pytest.param([1, 6, 1, 24], [1, 6, 1, 24], id="No zeros"),
43+
# все элементы нули
44+
pytest.param([0, 0, 0], [0, 0, 0], id="All zeros"),
45+
# один элемент - ноль
46+
pytest.param([0], [0], id="Single Zero"),
47+
# один элемент - не ноль
48+
pytest.param([5], [5], id="Single Non-Zero"),
49+
)
50+
51+
52+
@pytest.mark.parametrize(("nums", "ans"), test_cases)
53+
def test_success_v0(nums: list[int], ans: set[int], solution: Solution):
54+
nums_id = id(nums)
55+
solution.moveZeroes(nums)
56+
assert nums == ans
57+
assert id(nums) == nums_id
58+
59+
60+
@pytest.mark.parametrize(("nums", "ans"), test_cases)
61+
def test_success_v1(nums: list[int], ans: set[int], solution: Solution):
62+
nums_id = id(nums)
63+
solution.moveZeroesV1(nums)
64+
assert nums == ans
65+
assert id(nums) == nums_id
66+
67+
68+
@pytest.mark.parametrize(("nums", "ans"), test_cases)
69+
def test_success_v2(nums: list[int], ans: set[int], solution: Solution):
70+
nums_id = id(nums)
71+
solution.moveZeroesV2(nums)
72+
assert nums == ans
73+
assert nums == ans
74+
assert id(nums) == nums_id
75+
76+
77+
@pytest.fixture
78+
def solution() -> Solution:
79+
return Solution()

python_solutions/lc_283_move_zeroes/test_lc_283_move_zeroes.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)