Skip to content

Commit 3edf84b

Browse files
authored
feat: update solutions to lc problems: No.2149,2150 (#3280)
* No.2149.Rearrange Array Elements by Sign * No.2150.Find All Lonely Numbers in the Array
1 parent fecf7d9 commit 3edf84b

File tree

14 files changed

+203
-181
lines changed

14 files changed

+203
-181
lines changed

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ nums 中的正整数是 [3,1,2] ,负整数是 [-2,-5,-4] 。
7676

7777
<!-- solution:start -->
7878

79-
### 方法一
79+
### 方法一:双指针
80+
81+
我们先创建一个长度为 $n$ 的数组 $\textit{ans}$,然后使用两个指针 $i$ 和 $j$ 分别指向 $\textit{ans}$ 的偶数下标和奇数下标,初始时 $i = 0$, $j = 1$。
82+
83+
遍历数组 $\textit{nums}$,如果当前元素 $x$ 为正整数,则将 $x$ 放入 $\textit{ans}[i]$,并将 $i$ 增加 $2$;否则将 $x$ 放入 $\textit{ans}[j]$,并将 $j$ 增加 $2$。
84+
85+
最后返回 $\textit{ans}$ 即可。
86+
87+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8088

8189
<!-- tabs:start -->
8290

@@ -87,12 +95,12 @@ class Solution:
8795
def rearrangeArray(self, nums: List[int]) -> List[int]:
8896
ans = [0] * len(nums)
8997
i, j = 0, 1
90-
for num in nums:
91-
if num > 0:
92-
ans[i] = num
98+
for x in nums:
99+
if x > 0:
100+
ans[i] = x
93101
i += 2
94102
else:
95-
ans[j] = num
103+
ans[j] = x
96104
j += 2
97105
return ans
98106
```
@@ -101,16 +109,15 @@ class Solution:
101109

102110
```java
103111
class Solution {
104-
105112
public int[] rearrangeArray(int[] nums) {
106113
int[] ans = new int[nums.length];
107114
int i = 0, j = 1;
108-
for (int num : nums) {
109-
if (num > 0) {
110-
ans[i] = num;
115+
for (int x : nums) {
116+
if (x > 0) {
117+
ans[i] = x;
111118
i += 2;
112119
} else {
113-
ans[j] = num;
120+
ans[j] = x;
114121
j += 2;
115122
}
116123
}
@@ -127,12 +134,12 @@ public:
127134
vector<int> rearrangeArray(vector<int>& nums) {
128135
vector<int> ans(nums.size());
129136
int i = 0, j = 1;
130-
for (int num : nums) {
131-
if (num > 0) {
132-
ans[i] = num;
137+
for (int x : nums) {
138+
if (x > 0) {
139+
ans[i] = x;
133140
i += 2;
134141
} else {
135-
ans[j] = num;
142+
ans[j] = x;
136143
j += 2;
137144
}
138145
}
@@ -147,12 +154,12 @@ public:
147154
func rearrangeArray(nums []int) []int {
148155
ans := make([]int, len(nums))
149156
i, j := 0, 1
150-
for _, num := range nums {
151-
if num > 0 {
152-
ans[i] = num
157+
for _, x := range nums {
158+
if x > 0 {
159+
ans[i] = x
153160
i += 2
154161
} else {
155-
ans[j] = num
162+
ans[j] = x
156163
j += 2
157164
}
158165
}
@@ -164,15 +171,14 @@ func rearrangeArray(nums []int) []int {
164171

165172
```ts
166173
function rearrangeArray(nums: number[]): number[] {
167-
let ans = [];
168-
let i = 0,
169-
j = 1;
170-
for (let num of nums) {
171-
if (num > 0) {
172-
ans[i] = num;
174+
const ans: number[] = Array(nums.length);
175+
let [i, j] = [0, 1];
176+
for (const x of nums) {
177+
if (x > 0) {
178+
ans[i] = x;
173179
i += 2;
174180
} else {
175-
ans[j] = num;
181+
ans[j] = x;
176182
j += 2;
177183
}
178184
}

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ It is not required to do the modifications in-place.
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Two Pointers
77+
78+
First, we create an array $\textit{ans}$ of length $n$. Then, we use two pointers $i$ and $j$ to point to the even and odd indices of $\textit{ans}$, respectively, with initial values $i = 0$, $j = 1$.
79+
80+
We iterate through the array $\textit{nums}$. If the current element $x$ is a positive integer, then we place $x$ into $\textit{ans}[i]$ and increase $i$ by $2$; otherwise, we place $x$ into $\textit{ans}[j]$ and increase $j$ by $2$.
81+
82+
Finally, we return $\textit{ans}$.
83+
84+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
7785

7886
<!-- tabs:start -->
7987

@@ -84,12 +92,12 @@ class Solution:
8492
def rearrangeArray(self, nums: List[int]) -> List[int]:
8593
ans = [0] * len(nums)
8694
i, j = 0, 1
87-
for num in nums:
88-
if num > 0:
89-
ans[i] = num
95+
for x in nums:
96+
if x > 0:
97+
ans[i] = x
9098
i += 2
9199
else:
92-
ans[j] = num
100+
ans[j] = x
93101
j += 2
94102
return ans
95103
```
@@ -98,16 +106,15 @@ class Solution:
98106

99107
```java
100108
class Solution {
101-
102109
public int[] rearrangeArray(int[] nums) {
103110
int[] ans = new int[nums.length];
104111
int i = 0, j = 1;
105-
for (int num : nums) {
106-
if (num > 0) {
107-
ans[i] = num;
112+
for (int x : nums) {
113+
if (x > 0) {
114+
ans[i] = x;
108115
i += 2;
109116
} else {
110-
ans[j] = num;
117+
ans[j] = x;
111118
j += 2;
112119
}
113120
}
@@ -124,12 +131,12 @@ public:
124131
vector<int> rearrangeArray(vector<int>& nums) {
125132
vector<int> ans(nums.size());
126133
int i = 0, j = 1;
127-
for (int num : nums) {
128-
if (num > 0) {
129-
ans[i] = num;
134+
for (int x : nums) {
135+
if (x > 0) {
136+
ans[i] = x;
130137
i += 2;
131138
} else {
132-
ans[j] = num;
139+
ans[j] = x;
133140
j += 2;
134141
}
135142
}
@@ -144,12 +151,12 @@ public:
144151
func rearrangeArray(nums []int) []int {
145152
ans := make([]int, len(nums))
146153
i, j := 0, 1
147-
for _, num := range nums {
148-
if num > 0 {
149-
ans[i] = num
154+
for _, x := range nums {
155+
if x > 0 {
156+
ans[i] = x
150157
i += 2
151158
} else {
152-
ans[j] = num
159+
ans[j] = x
153160
j += 2
154161
}
155162
}
@@ -161,15 +168,14 @@ func rearrangeArray(nums []int) []int {
161168

162169
```ts
163170
function rearrangeArray(nums: number[]): number[] {
164-
let ans = [];
165-
let i = 0,
166-
j = 1;
167-
for (let num of nums) {
168-
if (num > 0) {
169-
ans[i] = num;
171+
const ans: number[] = Array(nums.length);
172+
let [i, j] = [0, 1];
173+
for (const x of nums) {
174+
if (x > 0) {
175+
ans[i] = x;
170176
i += 2;
171177
} else {
172-
ans[j] = num;
178+
ans[j] = x;
173179
j += 2;
174180
}
175181
}

solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ class Solution {
33
vector<int> rearrangeArray(vector<int>& nums) {
44
vector<int> ans(nums.size());
55
int i = 0, j = 1;
6-
for (int num : nums) {
7-
if (num > 0) {
8-
ans[i] = num;
6+
for (int x : nums) {
7+
if (x > 0) {
8+
ans[i] = x;
99
i += 2;
1010
} else {
11-
ans[j] = num;
11+
ans[j] = x;
1212
j += 2;
1313
}
1414
}

solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
func rearrangeArray(nums []int) []int {
22
ans := make([]int, len(nums))
33
i, j := 0, 1
4-
for _, num := range nums {
5-
if num > 0 {
6-
ans[i] = num
4+
for _, x := range nums {
5+
if x > 0 {
6+
ans[i] = x
77
i += 2
88
} else {
9-
ans[j] = num
9+
ans[j] = x
1010
j += 2
1111
}
1212
}

solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
2-
32
public int[] rearrangeArray(int[] nums) {
43
int[] ans = new int[nums.length];
54
int i = 0, j = 1;
6-
for (int num : nums) {
7-
if (num > 0) {
8-
ans[i] = num;
5+
for (int x : nums) {
6+
if (x > 0) {
7+
ans[i] = x;
98
i += 2;
109
} else {
11-
ans[j] = num;
10+
ans[j] = x;
1211
j += 2;
1312
}
1413
}

solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ class Solution:
22
def rearrangeArray(self, nums: List[int]) -> List[int]:
33
ans = [0] * len(nums)
44
i, j = 0, 1
5-
for num in nums:
6-
if num > 0:
7-
ans[i] = num
5+
for x in nums:
6+
if x > 0:
7+
ans[i] = x
88
i += 2
99
else:
10-
ans[j] = num
10+
ans[j] = x
1111
j += 2
1212
return ans

solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
function rearrangeArray(nums: number[]): number[] {
2-
let ans = [];
3-
let i = 0,
4-
j = 1;
5-
for (let num of nums) {
6-
if (num > 0) {
7-
ans[i] = num;
2+
const ans: number[] = Array(nums.length);
3+
let [i, j] = [0, 1];
4+
for (const x of nums) {
5+
if (x > 0) {
6+
ans[i] = x;
87
i += 2;
98
} else {
10-
ans[j] = num;
9+
ans[j] = x;
1110
j += 2;
1211
}
1312
}

0 commit comments

Comments
 (0)