File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
solution/0100-0199/0189.Rotate Array Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -174,7 +174,35 @@ impl Solution {
174
174
}
175
175
}
176
176
```
177
+ ### ** JavaScript**
178
+
179
+ <!-- 这里可写当前语言的特殊实现逻辑 -->
180
+
181
+ 使用三次数组翻转
177
182
183
+ ``` js
184
+ /**
185
+ * @param {number[]} nums
186
+ * @param {number} k
187
+ * @return {void} Do not return anything, modify nums in-place instead.
188
+ */
189
+ var rotate = function (nums , k ) {
190
+ k%= nums .length ;
191
+ reverse (nums,0 ,nums .length - 1 );
192
+ reverse (nums,0 ,k- 1 );
193
+ reverse (nums,k,nums .length - 1 );
194
+
195
+ };
196
+ function reverse (nums ,start ,end ){
197
+ while (start< end){
198
+ const temp = nums[start];
199
+ nums[start]= nums[end];
200
+ nums[end]= temp;
201
+ start+= 1 ;
202
+ end-= 1 ;
203
+ }
204
+ }
205
+ ```
178
206
### ** ...**
179
207
180
208
```
Original file line number Diff line number Diff line change @@ -150,6 +150,34 @@ impl Solution {
150
150
}
151
151
```
152
152
153
+ ### ** JavaScript**
154
+
155
+ Use three array flips
156
+
157
+ ``` js
158
+ /**
159
+ * @param {number[]} nums
160
+ * @param {number} k
161
+ * @return {void} Do not return anything, modify nums in-place instead.
162
+ */
163
+ var rotate = function (nums , k ) {
164
+ k%= nums .length ;
165
+ reverse (nums,0 ,nums .length - 1 );
166
+ reverse (nums,0 ,k- 1 );
167
+ reverse (nums,k,nums .length - 1 );
168
+
169
+ };
170
+ function reverse (nums ,start ,end ){
171
+ while (start< end){
172
+ const temp = nums[start];
173
+ nums[start]= nums[end];
174
+ nums[end]= temp;
175
+ start+= 1 ;
176
+ end-= 1 ;
177
+ }
178
+ }
179
+ ```
180
+
153
181
### ** ...**
154
182
155
183
```
You can’t perform that action at this time.
0 commit comments