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 9388678

Browse files
Merge pull request MisterBooo#114 from ztianming/patch-7
Update LeetCode第283号问题:移动零.md
2 parents f43c086 + 966ccbe commit 9388678

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

‎notes/LeetCode第283号问题:移动零.md‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ public:
103103

104104
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/gcetr.gif)
105105
代码如下:
106+
C++ Code:
106107

107-
```
108+
```c++
108109
// 原地(in place)解决该问题
109110
// 时间复杂度: O(n)
110111
// 空间复杂度: O(1)
@@ -130,8 +131,45 @@ public:
130131

131132
```
132133

134+
Java Code:
135+
136+
```java
137+
class Solution {
138+
public void moveZeroes(int[] nums) {
139+
// 双指针
140+
int i = 0;
141+
for(int j=0; j<nums.length; j++)
142+
{
143+
// 不为0,前移
144+
if(nums[j] != 0)
145+
{
146+
int temp = nums[i];
147+
nums[i] = nums[j];
148+
nums[j] = temp;
149+
i++;
150+
}
151+
}
152+
}
153+
}
154+
```
133155

156+
Python Code:
157+
158+
```python
159+
class Solution:
160+
def moveZeroes(self, nums: List[int]) -> None:
161+
"""
162+
Do not return anything, modify nums in-place instead.
163+
"""
164+
# 双指针
165+
i = 0
166+
for j in range(len(nums)):
167+
# 不为0,前移
168+
if nums[j] != 0:
169+
nums[i], nums[j] = nums[j], nums[i]
170+
i+=1
171+
```
134172

135173

136174

137-
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/o6der.png)
175+
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/o6der.png)

0 commit comments

Comments
(0)

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