File tree 7 files changed +142
-4
lines changed
0167.Two Sum II - Input array is sorted
0200-0299/0283.Move Zeroes
7 files changed +142
-4
lines changed Original file line number Diff line number Diff line change @@ -148,6 +148,35 @@ func twoSum(numbers []int, target int) []int {
148
148
}
149
149
```
150
150
151
+ ### ** JavaScript**
152
+
153
+ ``` js
154
+ /**
155
+ * @param {number[]} numbers
156
+ * @param {number} target
157
+ * @return {number[]}
158
+ */
159
+ var twoSum = function (numbers , target ) {
160
+ let left = 0 ;
161
+ let right = numbers .length - 1 ;
162
+ while (right > 0 ) {
163
+ let tem = target - numbers[right];
164
+ while (left < right) {
165
+ if (numbers[left] == tem) {
166
+ break ;
167
+ }
168
+ left++ ;
169
+ }
170
+ if (left < right) {
171
+ break ;
172
+ }
173
+ left = 0 ;
174
+ right-- ;
175
+ }
176
+ return [left + 1 , right + 1 ];
177
+ };
178
+ ```
179
+
151
180
### ** ...**
152
181
153
182
```
Original file line number Diff line number Diff line change @@ -137,6 +137,35 @@ func twoSum(numbers []int, target int) []int {
137
137
}
138
138
```
139
139
140
+ ### ** JavaScript**
141
+
142
+ ``` js
143
+ /**
144
+ * @param {number[]} numbers
145
+ * @param {number} target
146
+ * @return {number[]}
147
+ */
148
+ var twoSum = function (numbers , target ) {
149
+ let left = 0 ;
150
+ let right = numbers .length - 1 ;
151
+ while (right > 0 ) {
152
+ let tem = target - numbers[right];
153
+ while (left < right) {
154
+ if (numbers[left] == tem) {
155
+ break ;
156
+ }
157
+ left++ ;
158
+ }
159
+ if (left < right) {
160
+ break ;
161
+ }
162
+ left = 0 ;
163
+ right-- ;
164
+ }
165
+ return [left + 1 , right + 1 ];
166
+ };
167
+ ```
168
+
140
169
### ** ...**
141
170
142
171
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } numbers
3
+ * @param {number } target
4
+ * @return {number[] }
5
+ */
6
+ var twoSum = function ( numbers , target ) {
7
+ let left = 0 ;
8
+ let right = numbers . length - 1 ;
9
+ while ( right > 0 ) {
10
+ let tem = target - numbers [ right ] ;
11
+ while ( left < right ) {
12
+ if ( numbers [ left ] == tem ) {
13
+ break ;
14
+ }
15
+ left ++ ;
16
+ }
17
+ if ( left < right ) {
18
+ break ;
19
+ }
20
+ left = 0 ;
21
+ right -- ;
22
+ }
23
+ return [ left + 1 , right + 1 ] ;
24
+ } ;
Original file line number Diff line number Diff line change @@ -150,7 +150,6 @@ var rotate = function (nums, k) {
150
150
reverse (nums, 0 , nums .length - 1 );
151
151
reverse (nums, 0 , k - 1 );
152
152
reverse (nums, k, nums .length - 1 );
153
-
154
153
};
155
154
function reverse (nums , start , end ) {
156
155
// 双指针实现翻转
Original file line number Diff line number Diff line change @@ -99,7 +99,7 @@ class Solution {
99
99
100
100
### ** JavaScript**
101
101
102
- the elements in the range ` k~n-1 ` of the array to the front with the native API.
102
+ the elements in the range ` k~n-1 ` of the array to the front with the native API.
103
103
104
104
``` js
105
105
/**
@@ -121,13 +121,12 @@ Use three array reverses implemented by double pointers
121
121
* @param {number} k
122
122
* @return {void} Do not return anything, modify nums in-place instead.
123
123
*/
124
- var rotate = function (nums , k ) {
124
+ var rotate = function (nums , k ) {
125
125
k %= nums .length ;
126
126
// Use three array reverses
127
127
reverse (nums, 0 , nums .length - 1 );
128
128
reverse (nums, 0 , k - 1 );
129
129
reverse (nums, k, nums .length - 1 );
130
-
131
130
};
132
131
function reverse (nums , start , end ) {
133
132
// reverse implemented by double pointers
Original file line number Diff line number Diff line change @@ -115,6 +115,35 @@ var moveZeroes = function (nums) {
115
115
};
116
116
```
117
117
118
+ ``` js
119
+ /**
120
+ * @param {number[]} nums
121
+ * @return {void} Do not return anything, modify nums in-place instead.
122
+ */
123
+ var moveZeroes = function (nums ) {
124
+ let left = 0 ;
125
+ let right = left;
126
+ while (left < nums .length ) {
127
+ if (nums[left] != 0 ) {
128
+ left++ ;
129
+ } else {
130
+ right = left + 1 ;
131
+ while (right < nums .length ) {
132
+ if (nums[right] == 0 ) {
133
+ right++ ;
134
+ } else {
135
+ let tem = nums[left];
136
+ nums[left] = nums[right];
137
+ nums[right] = tem;
138
+ break ;
139
+ }
140
+ }
141
+ left++ ;
142
+ }
143
+ }
144
+ };
145
+ ```
146
+
118
147
### ** Rust**
119
148
120
149
``` rust
Original file line number Diff line number Diff line change @@ -116,6 +116,35 @@ var moveZeroes = function (nums) {
116
116
};
117
117
```
118
118
119
+ ``` js
120
+ /**
121
+ * @param {number[]} nums
122
+ * @return {void} Do not return anything, modify nums in-place instead.
123
+ */
124
+ var moveZeroes = function (nums ) {
125
+ let left = 0 ;
126
+ let right = left;
127
+ while (left < nums .length ) {
128
+ if (nums[left] != 0 ) {
129
+ left++ ;
130
+ } else {
131
+ right = left + 1 ;
132
+ while (right < nums .length ) {
133
+ if (nums[right] == 0 ) {
134
+ right++ ;
135
+ } else {
136
+ let tem = nums[left];
137
+ nums[left] = nums[right];
138
+ nums[right] = tem;
139
+ break ;
140
+ }
141
+ }
142
+ left++ ;
143
+ }
144
+ }
145
+ };
146
+ ```
147
+
119
148
### ** Rust**
120
149
121
150
``` rust
You can’t perform that action at this time.
0 commit comments