File tree 1 file changed +40
-2
lines changed 1 file changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -103,8 +103,9 @@ public:
103
103
104
104
![ ] ( https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/gcetr.gif )
105
105
代码如下:
106
+ C++ Code:
106
107
107
- ```
108
+ ``` c++
108
109
// 原地(in place)解决该问题
109
110
// 时间复杂度: O(n)
110
111
// 空间复杂度: O(1)
@@ -130,8 +131,45 @@ public:
130
131
131
132
```
132
133
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
+ ```
133
155
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
+ ```
134
172
135
173
136
174
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