Skip to content

Commit d943904

Browse files
committed
feat: add solutions to lc problem: No.1441
No.1441.Build an Array With Stack Operations
1 parent 0b012e3 commit d943904

File tree

8 files changed

+212
-3
lines changed

8 files changed

+212
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
- [被围绕的区域](./solution/0100-0199/0130.Surrounded%20Regions/README.md)
218218
- [省份数量](./solution/0500-0599/0547.Number%20of%20Provinces/README.md)
219219
- [冗余连接](./solution/0600-0699/0684.Redundant%20Connection/README.md)
220+
- [可能的二分法](./solution/0800-0899/0886.Possible%20Bipartition/README.md)
220221

221222
### 设计
222223

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
209209
- [Surrounded Regions](./solution/0100-0199/0130.Surrounded%20Regions/README_EN.md)
210210
- [Number of Provinces](./solution/0500-0599/0547.Number%20of%20Provinces/README_EN.md)
211211
- [Redundant Connection](./solution/0600-0699/0684.Redundant%20Connection/README_EN.md)
212+
- [Possible Bipartition](./solution/0800-0899/0886.Possible%20Bipartition/README_EN.md)
212213

213214
### Design
214215

solution/1400-1499/1441.Build an Array With Stack Operations/README.md

+73-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
<li><code>target</code> 是严格递增的</li>
6969
</ul>
7070

71-
7271
## 解法
7372

7473
<!-- 这里可写通用的实现逻辑 -->
@@ -80,15 +79,87 @@
8079
<!-- 这里可写当前语言的特殊实现逻辑 -->
8180

8281
```python
83-
82+
class Solution:
83+
def buildArray(self, target: List[int], n: int) -> List[str]:
84+
cur, ans = 1, []
85+
for t in target:
86+
for i in range(cur, n + 1):
87+
ans.append('Push')
88+
if t == i:
89+
cur = i + 1
90+
break
91+
ans.append('Pop')
92+
return ans
8493
```
8594

8695
### **Java**
8796

8897
<!-- 这里可写当前语言的特殊实现逻辑 -->
8998

9099
```java
100+
class Solution {
101+
public List<String> buildArray(int[] target, int n) {
102+
List<String> ans = new ArrayList<>();
103+
int cur = 1;
104+
for (int t : target) {
105+
for (int i = cur; i <= n; ++i) {
106+
ans.add("Push");
107+
if (t == i) {
108+
cur = i + 1;
109+
break;
110+
}
111+
ans.add("Pop");
112+
}
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<string> buildArray(vector<int>& target, int n) {
125+
vector<string> ans;
126+
int cur = 1;
127+
for (int t : target)
128+
{
129+
for (int i = cur; i <= n; ++i)
130+
{
131+
ans.push_back("Push");
132+
if (t == i)
133+
{
134+
cur = i + 1;
135+
break;
136+
}
137+
ans.push_back("Pop");
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
91144
145+
### **Go**
146+
147+
```go
148+
func buildArray(target []int, n int) []string {
149+
var ans []string
150+
cur := 1
151+
for _, t := range target {
152+
for i := cur; i <= n; i++ {
153+
ans = append(ans, "Push")
154+
if t == i {
155+
cur = i + 1
156+
break
157+
}
158+
ans = append(ans, "Pop")
159+
}
160+
}
161+
return ans
162+
}
92163
```
93164

94165
### **...**

solution/1400-1499/1441.Build an Array With Stack Operations/README_EN.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,85 @@ Read number 3 and automatically push in the array -&gt; [1,3]
6868
### **Python3**
6969

7070
```python
71-
71+
class Solution:
72+
def buildArray(self, target: List[int], n: int) -> List[str]:
73+
cur, ans = 1, []
74+
for t in target:
75+
for i in range(cur, n + 1):
76+
ans.append('Push')
77+
if t == i:
78+
cur = i + 1
79+
break
80+
ans.append('Pop')
81+
return ans
7282
```
7383

7484
### **Java**
7585

7686
```java
87+
class Solution {
88+
public List<String> buildArray(int[] target, int n) {
89+
List<String> ans = new ArrayList<>();
90+
int cur = 1;
91+
for (int t : target) {
92+
for (int i = cur; i <= n; ++i) {
93+
ans.add("Push");
94+
if (t == i) {
95+
cur = i + 1;
96+
break;
97+
}
98+
ans.add("Pop");
99+
}
100+
}
101+
return ans;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
vector<string> buildArray(vector<int>& target, int n) {
112+
vector<string> ans;
113+
int cur = 1;
114+
for (int t : target)
115+
{
116+
for (int i = cur; i <= n; ++i)
117+
{
118+
ans.push_back("Push");
119+
if (t == i)
120+
{
121+
cur = i + 1;
122+
break;
123+
}
124+
ans.push_back("Pop");
125+
}
126+
}
127+
return ans;
128+
}
129+
};
130+
```
77131
132+
### **Go**
133+
134+
```go
135+
func buildArray(target []int, n int) []string {
136+
var ans []string
137+
cur := 1
138+
for _, t := range target {
139+
for i := cur; i <= n; i++ {
140+
ans = append(ans, "Push")
141+
if t == i {
142+
cur = i + 1
143+
break
144+
}
145+
ans = append(ans, "Pop")
146+
}
147+
}
148+
return ans
149+
}
78150
```
79151

80152
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<string> buildArray(vector<int>& target, int n) {
4+
vector<string> ans;
5+
int cur = 1;
6+
for (int t : target)
7+
{
8+
for (int i = cur; i <= n; ++i)
9+
{
10+
ans.push_back("Push");
11+
if (t == i)
12+
{
13+
cur = i + 1;
14+
break;
15+
}
16+
ans.push_back("Pop");
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func buildArray(target []int, n int) []string {
2+
var ans []string
3+
cur := 1
4+
for _, t := range target {
5+
for i := cur; i <= n; i++ {
6+
ans = append(ans, "Push")
7+
if t == i {
8+
cur = i + 1
9+
break
10+
}
11+
ans = append(ans, "Pop")
12+
}
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<String> buildArray(int[] target, int n) {
3+
List<String> ans = new ArrayList<>();
4+
int cur = 1;
5+
for (int t : target) {
6+
for (int i = cur; i <= n; ++i) {
7+
ans.add("Push");
8+
if (t == i) {
9+
cur = i + 1;
10+
break;
11+
}
12+
ans.add("Pop");
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def buildArray(self, target: List[int], n: int) -> List[str]:
3+
cur, ans = 1, []
4+
for t in target:
5+
for i in range(cur, n + 1):
6+
ans.append('Push')
7+
if t == i:
8+
cur = i + 1
9+
break
10+
ans.append('Pop')
11+
return ans

0 commit comments

Comments
 (0)