File tree 2 files changed +22
-22
lines changed
solution/0100-0199/0189.Rotate Array
2 files changed +22
-22
lines changed Original file line number Diff line number Diff line change @@ -144,21 +144,21 @@ var rotate = function (nums, k) {
144
144
* @param {number} k
145
145
* @return {void} Do not return anything, modify nums in-place instead.
146
146
*/
147
- var rotate = function (nums , k ) {
148
- k%= nums .length ;
149
- // 使用三次数组翻转
150
- reverse (nums,0 , nums .length - 1 );
151
- reverse (nums,0 , k- 1 );
152
- reverse (nums,k, nums .length - 1 );
147
+ var rotate = function (nums , k ) {
148
+ k %= nums .length ;
149
+ // 使用三次数组翻转
150
+ reverse (nums, 0 , nums .length - 1 );
151
+ reverse (nums, 0 , k- 1 );
152
+ reverse (nums, k, nums .length - 1 );
153
153
154
154
};
155
- function reverse (nums ,start ,end ){
156
- while (start< end){
155
+ function reverse (nums , start , end ){
156
+ while (start < end){
157
157
const temp = nums[start];
158
- nums[start]= nums[end];
159
- nums[end]= temp;
160
- start+= 1 ;
161
- end-= 1 ;
158
+ nums[start] = nums[end];
159
+ nums[end] = temp;
160
+ start += 1 ;
161
+ end -= 1 ;
162
162
}
163
163
}
164
164
```
Original file line number Diff line number Diff line change @@ -121,20 +121,20 @@ Use three array flips
121
121
* @return {void} Do not return anything, modify nums in-place instead.
122
122
*/
123
123
var rotate = function (nums , k ) {
124
- k%= nums .length ;
124
+ k %= nums .length ;
125
125
// Use three array flips
126
- reverse (nums,0 , nums .length - 1 );
127
- reverse (nums,0 , k- 1 );
128
- reverse (nums,k, nums .length - 1 );
126
+ reverse (nums, 0 , nums .length - 1 );
127
+ reverse (nums, 0 , k- 1 );
128
+ reverse (nums, k, nums .length - 1 );
129
129
130
130
};
131
- function reverse (nums ,start ,end ){
132
- while (start< end){
131
+ function reverse (nums , start , end ){
132
+ while (start < end){
133
133
const temp = nums[start];
134
- nums[start]= nums[end];
135
- nums[end]= temp;
136
- start+= 1 ;
137
- end-= 1 ;
134
+ nums[start] = nums[end];
135
+ nums[end] = temp;
136
+ start += 1 ;
137
+ end -= 1 ;
138
138
}
139
139
}
140
140
```
You can’t perform that action at this time.
0 commit comments