Skip to content

feat: update solutions to lc problems: No.2149,2150 #3280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ nums 中的正整数是 [3,1,2] ,负整数是 [-2,-5,-4] 。

<!-- solution:start -->

### 方法一
### 方法一:双指针

我们先创建一个长度为 $n$ 的数组 $\textit{ans}$,然后使用两个指针 $i$ 和 $j$ 分别指向 $\textit{ans}$ 的偶数下标和奇数下标,初始时 $i = 0$, $j = 1$。

遍历数组 $\textit{nums}$,如果当前元素 $x$ 为正整数,则将 $x$ 放入 $\textit{ans}[i]$,并将 $i$ 增加 $2$;否则将 $x$ 放入 $\textit{ans}[j]$,并将 $j$ 增加 $2$。

最后返回 $\textit{ans}$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

Expand All @@ -87,12 +95,12 @@ class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
ans = [0] * len(nums)
i, j = 0, 1
for num in nums:
if num > 0:
ans[i] = num
for x in nums:
if x > 0:
ans[i] = x
i += 2
else:
ans[j] = num
ans[j] = x
j += 2
return ans
```
Expand All @@ -101,16 +109,15 @@ class Solution:

```java
class Solution {

public int[] rearrangeArray(int[] nums) {
int[] ans = new int[nums.length];
int i = 0, j = 1;
for (int num : nums) {
if (num > 0) {
ans[i] = num;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand All @@ -127,12 +134,12 @@ public:
vector<int> rearrangeArray(vector<int>& nums) {
vector<int> ans(nums.size());
int i = 0, j = 1;
for (int num : nums) {
if (num > 0) {
ans[i] = num;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand All @@ -147,12 +154,12 @@ public:
func rearrangeArray(nums []int) []int {
ans := make([]int, len(nums))
i, j := 0, 1
for _, num := range nums {
if num > 0 {
ans[i] = num
for _, x := range nums {
if x > 0 {
ans[i] = x
i += 2
} else {
ans[j] = num
ans[j] = x
j += 2
}
}
Expand All @@ -164,15 +171,14 @@ func rearrangeArray(nums []int) []int {

```ts
function rearrangeArray(nums: number[]): number[] {
let ans = [];
let i = 0,
j = 1;
for (let num of nums) {
if (num > 0) {
ans[i] = num;
const ans: number[] = Array(nums.length);
let [i, j] = [0, 1];
for (const x of nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ It is not required to do the modifications in-place.

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

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$.

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$.

Finally, we return $\textit{ans}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand All @@ -84,12 +92,12 @@ class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
ans = [0] * len(nums)
i, j = 0, 1
for num in nums:
if num > 0:
ans[i] = num
for x in nums:
if x > 0:
ans[i] = x
i += 2
else:
ans[j] = num
ans[j] = x
j += 2
return ans
```
Expand All @@ -98,16 +106,15 @@ class Solution:

```java
class Solution {

public int[] rearrangeArray(int[] nums) {
int[] ans = new int[nums.length];
int i = 0, j = 1;
for (int num : nums) {
if (num > 0) {
ans[i] = num;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand All @@ -124,12 +131,12 @@ public:
vector<int> rearrangeArray(vector<int>& nums) {
vector<int> ans(nums.size());
int i = 0, j = 1;
for (int num : nums) {
if (num > 0) {
ans[i] = num;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand All @@ -144,12 +151,12 @@ public:
func rearrangeArray(nums []int) []int {
ans := make([]int, len(nums))
i, j := 0, 1
for _, num := range nums {
if num > 0 {
ans[i] = num
for _, x := range nums {
if x > 0 {
ans[i] = x
i += 2
} else {
ans[j] = num
ans[j] = x
j += 2
}
}
Expand All @@ -161,15 +168,14 @@ func rearrangeArray(nums []int) []int {

```ts
function rearrangeArray(nums: number[]): number[] {
let ans = [];
let i = 0,
j = 1;
for (let num of nums) {
if (num > 0) {
ans[i] = num;
const ans: number[] = Array(nums.length);
let [i, j] = [0, 1];
for (const x of nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ class Solution {
vector<int> rearrangeArray(vector<int>& nums) {
vector<int> ans(nums.size());
int i = 0, j = 1;
for (int num : nums) {
if (num > 0) {
ans[i] = num;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
func rearrangeArray(nums []int) []int {
ans := make([]int, len(nums))
i, j := 0, 1
for _, num := range nums {
if num > 0 {
ans[i] = num
for _, x := range nums {
if x > 0 {
ans[i] = x
i += 2
} else {
ans[j] = num
ans[j] = x
j += 2
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class Solution {

public int[] rearrangeArray(int[] nums) {
int[] ans = new int[nums.length];
int i = 0, j = 1;
for (int num : nums) {
if (num > 0) {
ans[i] = num;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
ans = [0] * len(nums)
i, j = 0, 1
for num in nums:
if num > 0:
ans[i] = num
for x in nums:
if x > 0:
ans[i] = x
i += 2
else:
ans[j] = num
ans[j] = x
j += 2
return ans
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
function rearrangeArray(nums: number[]): number[] {
let ans = [];
let i = 0,
j = 1;
for (let num of nums) {
if (num > 0) {
ans[i] = num;
const ans: number[] = Array(nums.length);
let [i, j] = [0, 1];
for (const x of nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = num;
ans[j] = x;
j += 2;
}
}
Expand Down
Loading
Loading