Skip to content

Commit 9fb8d56

Browse files
authored
feat: add solutions to lc problems: No.3069,3070 (doocs#2406)
1 parent d1ee10f commit 9fb8d56

File tree

17 files changed

+490
-23
lines changed

17 files changed

+490
-23
lines changed

solution/0200-0299/0232.Implement Queue using Stacks/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ myQueue.empty(); // return false
7171

7272
### 方法一:双栈
7373

74-
使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。
74+
我们使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。
7575

7676
入队时,直接将元素入栈 `stk1`。时间复杂度 $O(1)$。
7777

@@ -282,7 +282,7 @@ class MyQueue {
282282

283283
peek(): number {
284284
this.move();
285-
return this.stk2[this.stk2.length - 1];
285+
return this.stk2.at(-1);
286286
}
287287

288288
empty(): boolean {
@@ -292,7 +292,7 @@ class MyQueue {
292292
move(): void {
293293
if (!this.stk2.length) {
294294
while (this.stk1.length) {
295-
this.stk2.push(this.stk1.pop());
295+
this.stk2.push(this.stk1.pop()!);
296296
}
297297
}
298298
}

solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class MyQueue {
260260

261261
peek(): number {
262262
this.move();
263-
return this.stk2[this.stk2.length - 1];
263+
return this.stk2.at(-1);
264264
}
265265

266266
empty(): boolean {
@@ -270,7 +270,7 @@ class MyQueue {
270270
move(): void {
271271
if (!this.stk2.length) {
272272
while (this.stk1.length) {
273-
this.stk2.push(this.stk1.pop());
273+
this.stk2.push(this.stk1.pop()!);
274274
}
275275
}
276276
}

solution/0200-0299/0232.Implement Queue using Stacks/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MyQueue {
1818

1919
peek(): number {
2020
this.move();
21-
return this.stk2[this.stk2.length - 1];
21+
return this.stk2.at(-1);
2222
}
2323

2424
empty(): boolean {
@@ -28,7 +28,7 @@ class MyQueue {
2828
move(): void {
2929
if (!this.stk2.length) {
3030
while (this.stk1.length) {
31-
this.stk2.push(this.stk1.pop());
31+
this.stk2.push(this.stk1.pop()!);
3232
}
3333
}
3434
}

solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README.md

+83-4
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,103 @@
5757

5858
## 解法
5959

60-
### 方法一
60+
### 方法一:模拟
61+
62+
我们创建两个数组 `arr1``arr2`,分别存放 `nums` 中的元素,初始时 `arr1` 中只有 `nums[0]``arr2` 中只有 `nums[1]`
63+
64+
然后遍历 `nums` 下标从 $2$ 开始的元素,如果 `arr1` 的最后一个元素大于 `arr2` 的最后一个元素,就将当前元素追加到 `arr1`,否则追加到 `arr2`
65+
66+
最后将 `arr2` 中的元素追加到 `arr1` 中,返回 `arr1`
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
6169

6270
<!-- tabs:start -->
6371

6472
```python
65-
73+
class Solution:
74+
def resultArray(self, nums: List[int]) -> List[int]:
75+
arr1 = [nums[0]]
76+
arr2 = [nums[1]]
77+
for x in nums[2:]:
78+
if arr1[-1] > arr2[-1]:
79+
arr1.append(x)
80+
else:
81+
arr2.append(x)
82+
return arr1 + arr2
6683
```
6784

6885
```java
69-
86+
class Solution {
87+
public int[] resultArray(int[] nums) {
88+
int n = nums.length;
89+
int[] arr1 = new int[n];
90+
int[] arr2 = new int[n];
91+
arr1[0] = nums[0];
92+
arr2[0] = nums[1];
93+
int i = 0, j = 0;
94+
for (int k = 2; k < n; ++k) {
95+
if (arr1[i] > arr2[j]) {
96+
arr1[++i] = nums[k];
97+
} else {
98+
arr2[++j] = nums[k];
99+
}
100+
}
101+
for (int k = 0; k <= j; ++k) {
102+
arr1[++i] = arr2[k];
103+
}
104+
return arr1;
105+
}
106+
}
70107
```
71108

72109
```cpp
73-
110+
class Solution {
111+
public:
112+
vector<int> resultArray(vector<int>& nums) {
113+
int n = nums.size();
114+
vector<int> arr1 = {nums[0]};
115+
vector<int> arr2 = {nums[1]};
116+
for (int k = 2; k < n; ++k) {
117+
if (arr1.back() > arr2.back()) {
118+
arr1.push_back(nums[k]);
119+
} else {
120+
arr2.push_back(nums[k]);
121+
}
122+
}
123+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
124+
return arr1;
125+
}
126+
};
74127
```
75128
76129
```go
130+
func resultArray(nums []int) []int {
131+
arr1 := []int{nums[0]}
132+
arr2 := []int{nums[1]}
133+
for _, x := range nums[2:] {
134+
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
135+
arr1 = append(arr1, x)
136+
} else {
137+
arr2 = append(arr2, x)
138+
}
139+
}
140+
return append(arr1, arr2...)
141+
}
142+
```
77143

144+
```ts
145+
function resultArray(nums: number[]): number[] {
146+
const arr1: number[] = [nums[0]];
147+
const arr2: number[] = [nums[1]];
148+
for (const x of nums.slice(2)) {
149+
if (arr1.at(-1)! > arr2.at(-1)!) {
150+
arr1.push(x);
151+
} else {
152+
arr2.push(x);
153+
}
154+
}
155+
return arr1.concat(arr2);
156+
}
78157
```
79158

80159
<!-- tabs:end -->

solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README_EN.md

+83-4
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,103 @@ Hence, the array result formed by concatenation is [5,3,4,8].
5353

5454
## Solutions
5555

56-
### Solution 1
56+
### Solution 1: Simulation
57+
58+
We create two arrays `arr1` and `arr2`, which store the elements in `nums`. Initially, `arr1` only contains `nums[0]`, and `arr2` only contains `nums[1]`.
59+
60+
Then we traverse the elements of `nums` starting from index 2. If the last element of `arr1` is greater than the last element of `arr2`, we append the current element to `arr1`, otherwise we append it to `arr2`.
61+
62+
Finally, we append the elements in `arr2` to `arr1` and return `arr1`.
63+
64+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `nums`.
5765

5866
<!-- tabs:start -->
5967

6068
```python
61-
69+
class Solution:
70+
def resultArray(self, nums: List[int]) -> List[int]:
71+
arr1 = [nums[0]]
72+
arr2 = [nums[1]]
73+
for x in nums[2:]:
74+
if arr1[-1] > arr2[-1]:
75+
arr1.append(x)
76+
else:
77+
arr2.append(x)
78+
return arr1 + arr2
6279
```
6380

6481
```java
65-
82+
class Solution {
83+
public int[] resultArray(int[] nums) {
84+
int n = nums.length;
85+
int[] arr1 = new int[n];
86+
int[] arr2 = new int[n];
87+
arr1[0] = nums[0];
88+
arr2[0] = nums[1];
89+
int i = 0, j = 0;
90+
for (int k = 2; k < n; ++k) {
91+
if (arr1[i] > arr2[j]) {
92+
arr1[++i] = nums[k];
93+
} else {
94+
arr2[++j] = nums[k];
95+
}
96+
}
97+
for (int k = 0; k <= j; ++k) {
98+
arr1[++i] = arr2[k];
99+
}
100+
return arr1;
101+
}
102+
}
66103
```
67104

68105
```cpp
69-
106+
class Solution {
107+
public:
108+
vector<int> resultArray(vector<int>& nums) {
109+
int n = nums.size();
110+
vector<int> arr1 = {nums[0]};
111+
vector<int> arr2 = {nums[1]};
112+
for (int k = 2; k < n; ++k) {
113+
if (arr1.back() > arr2.back()) {
114+
arr1.push_back(nums[k]);
115+
} else {
116+
arr2.push_back(nums[k]);
117+
}
118+
}
119+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
120+
return arr1;
121+
}
122+
};
70123
```
71124
72125
```go
126+
func resultArray(nums []int) []int {
127+
arr1 := []int{nums[0]}
128+
arr2 := []int{nums[1]}
129+
for _, x := range nums[2:] {
130+
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
131+
arr1 = append(arr1, x)
132+
} else {
133+
arr2 = append(arr2, x)
134+
}
135+
}
136+
return append(arr1, arr2...)
137+
}
138+
```
73139

140+
```ts
141+
function resultArray(nums: number[]): number[] {
142+
const arr1: number[] = [nums[0]];
143+
const arr2: number[] = [nums[1]];
144+
for (const x of nums.slice(2)) {
145+
if (arr1.at(-1)! > arr2.at(-1)!) {
146+
arr1.push(x);
147+
} else {
148+
arr2.push(x);
149+
}
150+
}
151+
return arr1.concat(arr2);
152+
}
74153
```
75154

76155
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> resultArray(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> arr1 = {nums[0]};
6+
vector<int> arr2 = {nums[1]};
7+
for (int k = 2; k < n; ++k) {
8+
if (arr1.back() > arr2.back()) {
9+
arr1.push_back(nums[k]);
10+
} else {
11+
arr2.push_back(nums[k]);
12+
}
13+
}
14+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
15+
return arr1;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func resultArray(nums []int) []int {
2+
arr1 := []int{nums[0]}
3+
arr2 := []int{nums[1]}
4+
for _, x := range nums[2:] {
5+
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
6+
arr1 = append(arr1, x)
7+
} else {
8+
arr2 = append(arr2, x)
9+
}
10+
}
11+
return append(arr1, arr2...)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int[] resultArray(int[] nums) {
3+
int n = nums.length;
4+
int[] arr1 = new int[n];
5+
int[] arr2 = new int[n];
6+
arr1[0] = nums[0];
7+
arr2[0] = nums[1];
8+
int i = 0, j = 0;
9+
for (int k = 2; k < n; ++k) {
10+
if (arr1[i] > arr2[j]) {
11+
arr1[++i] = nums[k];
12+
} else {
13+
arr2[++j] = nums[k];
14+
}
15+
}
16+
for (int k = 0; k <= j; ++k) {
17+
arr1[++i] = arr2[k];
18+
}
19+
return arr1;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def resultArray(self, nums: List[int]) -> List[int]:
3+
arr1 = [nums[0]]
4+
arr2 = [nums[1]]
5+
for x in nums[2:]:
6+
if arr1[-1] > arr2[-1]:
7+
arr1.append(x)
8+
else:
9+
arr2.append(x)
10+
return arr1 + arr2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function resultArray(nums: number[]): number[] {
2+
const arr1: number[] = [nums[0]];
3+
const arr2: number[] = [nums[1]];
4+
for (const x of nums.slice(2)) {
5+
if (arr1.at(-1)! > arr2.at(-1)!) {
6+
arr1.push(x);
7+
} else {
8+
arr2.push(x);
9+
}
10+
}
11+
return arr1.concat(arr2);
12+
}

0 commit comments

Comments
 (0)