Skip to content

Commit a8cc312

Browse files
authored
feat: add solutions to lc No.3936,3937 (#5223)
1 parent 9fa9101 commit a8cc312

14 files changed

Lines changed: 780 additions & 16 deletions

File tree

solution/3900-3999/3936.Minimum Swaps to Move Zeros to End/README.md

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,149 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3936.Mi
8686

8787
<!-- solution:start -->
8888

89-
### 方法一
89+
### 方法一:双指针
90+
91+
我们用两个指针 $i$ 和 $j$ 分别指向数组的开头和结尾。每次我们将 $i$ 向右移动直到找到一个 0,将 $j$ 向左移动直到找到一个非 0 的数。如果 $i < j$,我们就交换这两个数,并将答案加 1。重复这个过程直到 $i \geq j$。
92+
93+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
9094

9195
<!-- tabs:start -->
9296

9397
#### Python3
9498

9599
```python
96-
100+
class Solution:
101+
def minimumSwaps(self, nums: list[int]) -> int:
102+
ans = 0
103+
n = len(nums)
104+
i, j = 0, n - 1
105+
while i < j:
106+
while i < n and nums[i] != 0:
107+
i += 1
108+
while j and nums[j] == 0:
109+
j -= 1
110+
if i >= j:
111+
break
112+
ans += 1
113+
i += 1
114+
j -= 1
115+
return ans
97116
```
98117

99118
#### Java
100119

101120
```java
102-
121+
class Solution {
122+
public int minimumSwaps(int[] nums) {
123+
int ans = 0;
124+
int n = nums.length;
125+
for (int i = 0, j = n - 1; i < j; ++i, --j) {
126+
while (i < n && nums[i] != 0) {
127+
++i;
128+
}
129+
130+
while (j > 0 && nums[j] == 0) {
131+
--j;
132+
}
133+
134+
if (i >= j) {
135+
break;
136+
}
137+
138+
++ans;
139+
}
140+
141+
return ans;
142+
}
143+
}
103144
```
104145

105146
#### C++
106147

107148
```cpp
108-
149+
class Solution {
150+
public:
151+
int minimumSwaps(vector<int>& nums) {
152+
int ans = 0;
153+
int n = nums.size();
154+
for (int i = 0, j = n - 1; i < j; ++i, --j) {
155+
while (i < n && nums[i] != 0) {
156+
++i;
157+
}
158+
159+
while (j > 0 && nums[j] == 0) {
160+
--j;
161+
}
162+
163+
if (i >= j) {
164+
break;
165+
}
166+
167+
++ans;
168+
}
169+
170+
return ans;
171+
}
172+
};
109173
```
110174

111175
#### Go
112176

113177
```go
178+
func minimumSwaps(nums []int) int {
179+
ans := 0
180+
n := len(nums)
181+
182+
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
183+
for i < n && nums[i] != 0 {
184+
i++
185+
}
186+
187+
for j > 0 && nums[j] == 0 {
188+
j--
189+
}
190+
191+
if i >= j {
192+
break
193+
}
194+
195+
ans++
196+
}
197+
198+
return ans
199+
}
200+
```
201+
202+
#### TypeScript
203+
204+
```ts
205+
function minimumSwaps(nums: number[]): number {
206+
let ans = 0;
207+
const n = nums.length;
208+
209+
let i = 0;
210+
let j = n - 1;
211+
212+
while (i < j) {
213+
while (i < n && nums[i] !== 0) {
214+
++i;
215+
}
216+
217+
while (j > 0 && nums[j] === 0) {
218+
--j;
219+
}
220+
221+
if (i >= j) {
222+
break;
223+
}
224+
225+
++ans;
226+
++i;
227+
--j;
228+
}
114229

230+
return ans;
231+
}
115232
```
116233

117234
<!-- tabs:end -->

solution/3900-3999/3936.Minimum Swaps to Move Zeros to End/README_EN.md

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,149 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3936.Mi
8484

8585
<!-- solution:start -->
8686

87-
### Solution 1
87+
### Solution 1: Two Pointers
88+
89+
We use two pointers $i$ and $j$ pointing to the beginning and end of the array respectively. Each time, we move $i$ to the right until we find a 0, and move $j$ to the left until we find a non-zero number. If $i < j$, we swap the two elements and increment the answer by 1. We repeat this process until $i \geq j$.
90+
91+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
8892

8993
<!-- tabs:start -->
9094

9195
#### Python3
9296

9397
```python
94-
98+
class Solution:
99+
def minimumSwaps(self, nums: list[int]) -> int:
100+
ans = 0
101+
n = len(nums)
102+
i, j = 0, n - 1
103+
while i < j:
104+
while i < n and nums[i] != 0:
105+
i += 1
106+
while j and nums[j] == 0:
107+
j -= 1
108+
if i >= j:
109+
break
110+
ans += 1
111+
i += 1
112+
j -= 1
113+
return ans
95114
```
96115

97116
#### Java
98117

99118
```java
100-
119+
class Solution {
120+
public int minimumSwaps(int[] nums) {
121+
int ans = 0;
122+
int n = nums.length;
123+
for (int i = 0, j = n - 1; i < j; ++i, --j) {
124+
while (i < n && nums[i] != 0) {
125+
++i;
126+
}
127+
128+
while (j > 0 && nums[j] == 0) {
129+
--j;
130+
}
131+
132+
if (i >= j) {
133+
break;
134+
}
135+
136+
++ans;
137+
}
138+
139+
return ans;
140+
}
141+
}
101142
```
102143

103144
#### C++
104145

105146
```cpp
106-
147+
class Solution {
148+
public:
149+
int minimumSwaps(vector<int>& nums) {
150+
int ans = 0;
151+
int n = nums.size();
152+
for (int i = 0, j = n - 1; i < j; ++i, --j) {
153+
while (i < n && nums[i] != 0) {
154+
++i;
155+
}
156+
157+
while (j > 0 && nums[j] == 0) {
158+
--j;
159+
}
160+
161+
if (i >= j) {
162+
break;
163+
}
164+
165+
++ans;
166+
}
167+
168+
return ans;
169+
}
170+
};
107171
```
108172

109173
#### Go
110174

111175
```go
176+
func minimumSwaps(nums []int) int {
177+
ans := 0
178+
n := len(nums)
179+
180+
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
181+
for i < n && nums[i] != 0 {
182+
i++
183+
}
184+
185+
for j > 0 && nums[j] == 0 {
186+
j--
187+
}
188+
189+
if i >= j {
190+
break
191+
}
192+
193+
ans++
194+
}
195+
196+
return ans
197+
}
198+
```
199+
200+
#### TypeScript
201+
202+
```ts
203+
function minimumSwaps(nums: number[]): number {
204+
let ans = 0;
205+
const n = nums.length;
206+
207+
let i = 0;
208+
let j = n - 1;
209+
210+
while (i < j) {
211+
while (i < n && nums[i] !== 0) {
212+
++i;
213+
}
214+
215+
while (j > 0 && nums[j] === 0) {
216+
--j;
217+
}
218+
219+
if (i >= j) {
220+
break;
221+
}
222+
223+
++ans;
224+
++i;
225+
--j;
226+
}
112227

228+
return ans;
229+
}
113230
```
114231

115232
<!-- tabs:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int minimumSwaps(vector<int>& nums) {
4+
int ans = 0;
5+
int n = nums.size();
6+
for (int i = 0, j = n - 1; i < j; ++i, --j) {
7+
while (i < n && nums[i] != 0) {
8+
++i;
9+
}
10+
11+
while (j > 0 && nums[j] == 0) {
12+
--j;
13+
}
14+
15+
if (i >= j) {
16+
break;
17+
}
18+
19+
++ans;
20+
}
21+
22+
return ans;
23+
}
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func minimumSwaps(nums []int) int {
2+
ans := 0
3+
n := len(nums)
4+
5+
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
6+
for i < n && nums[i] != 0 {
7+
i++
8+
}
9+
10+
for j > 0 && nums[j] == 0 {
11+
j--
12+
}
13+
14+
if i >= j {
15+
break
16+
}
17+
18+
ans++
19+
}
20+
21+
return ans
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int minimumSwaps(int[] nums) {
3+
int ans = 0;
4+
int n = nums.length;
5+
for (int i = 0, j = n - 1; i < j; ++i, --j) {
6+
while (i < n && nums[i] != 0) {
7+
++i;
8+
}
9+
10+
while (j > 0 && nums[j] == 0) {
11+
--j;
12+
}
13+
14+
if (i >= j) {
15+
break;
16+
}
17+
18+
++ans;
19+
}
20+
21+
return ans;
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minimumSwaps(self, nums: list[int]) -> int:
3+
ans = 0
4+
n = len(nums)
5+
i, j = 0, n - 1
6+
while i < j:
7+
while i < n and nums[i] != 0:
8+
i += 1
9+
while j and nums[j] == 0:
10+
j -= 1
11+
if i >= j:
12+
break
13+
ans += 1
14+
i += 1
15+
j -= 1
16+
return ans

0 commit comments

Comments
 (0)