File tree Expand file tree Collapse file tree 1 file changed +40
-2
lines changed Expand file tree Collapse file tree 1 file changed +40
-2
lines changed Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments