Skip to content

Commit 5a5333d

Browse files
committed
feat: add typescript solution to lc problem: No.2149
No.2149.Rearrange Array Elements by Sign
1 parent 76747eb commit 5a5333d

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,20 @@ func rearrangeArray(nums []int) []int {
152152
<!-- 这里可写当前语言的特殊实现逻辑 -->
153153

154154
```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+
};
156169
```
157170

158171
### **...**

solution/2100-2199/2149.Rearrange Array Elements by Sign/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,20 @@ func rearrangeArray(nums []int) []int {
140140
### **TypeScript**
141141

142142
```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+
};
144157
```
145158

146159
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
};

0 commit comments

Comments
 (0)