File tree 3 files changed +42
-2
lines changed
solution/2100-2199/2149.Rearrange Array Elements by Sign
3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,20 @@ func rearrangeArray(nums []int) []int {
152
152
<!-- 这里可写当前语言的特殊实现逻辑 -->
153
153
154
154
``` ts
155
-
155
+ function rearrangeArray(nums : number []): number [] {
156
+ let ans = [];
157
+ let i = 0 , j = 1 ;
158
+ for (let num of nums ) {
159
+ if (num > 0 ) {
160
+ ans [i ] = num ;
161
+ i += 2 ;
162
+ } else {
163
+ ans [j ] = num ;
164
+ j += 2 ;
165
+ }
166
+ }
167
+ return ans ;
168
+ };
156
169
```
157
170
158
171
### ** ...**
Original file line number Diff line number Diff line change @@ -140,7 +140,20 @@ func rearrangeArray(nums []int) []int {
140
140
### ** TypeScript**
141
141
142
142
``` ts
143
-
143
+ function rearrangeArray(nums : number []): number [] {
144
+ let ans = [];
145
+ let i = 0 , j = 1 ;
146
+ for (let num of nums ) {
147
+ if (num > 0 ) {
148
+ ans [i ] = num ;
149
+ i += 2 ;
150
+ } else {
151
+ ans [j ] = num ;
152
+ j += 2 ;
153
+ }
154
+ }
155
+ return ans ;
156
+ };
144
157
```
145
158
146
159
### ** ...**
Original file line number Diff line number Diff line change
1
+ function rearrangeArray ( nums : number [ ] ) : number [ ] {
2
+ let ans = [ ] ;
3
+ let i = 0 , j = 1 ;
4
+ for ( let num of nums ) {
5
+ if ( num > 0 ) {
6
+ ans [ i ] = num ;
7
+ i += 2 ;
8
+ } else {
9
+ ans [ j ] = num ;
10
+ j += 2 ;
11
+ }
12
+ }
13
+ return ans ;
14
+ } ;
You can’t perform that action at this time.
0 commit comments