Skip to content

Commit 9659c6f

Browse files
committed
feat: add solutions to lc problem: No.1381
No.1381.Design a Stack With Increment Operation
1 parent 3057d66 commit 9659c6f

File tree

8 files changed

+272
-48
lines changed

8 files changed

+272
-48
lines changed

solution/0700-0799/0707.Design Linked List/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ linkedList.get(1); //返回3
5757
- e 存储链表节点的值
5858
- ne 存储链表节点的 next 指针
5959
- idx 指向当前可分配的节点下标
60+
- size 存储链表节点的个数
6061

6162
<!-- tabs:start -->
6263

solution/1300-1399/1381.Design a Stack With Increment Operation/README.md

+94-17
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,21 @@ class CustomStack:
6969

7070
def __init__(self, maxSize: int):
7171
self.s = [0] * maxSize
72-
self.tail = 0
72+
self.t = 0
7373

7474
def push(self, x: int) -> None:
75-
if self.tail < len(self.s):
76-
self.s[self.tail] = x
77-
self.tail += 1
75+
if self.t < len(self.s):
76+
self.s[self.t] = x
77+
self.t += 1
7878

7979
def pop(self) -> int:
80-
if self.tail == 0:
80+
if self.t == 0:
8181
return -1
82-
self.tail -= 1
83-
return self.s[self.tail]
82+
self.t -= 1
83+
return self.s[self.t]
8484

8585
def increment(self, k: int, val: int) -> None:
86-
for i in range(min(k, self.tail)):
86+
for i in range(min(k, self.t)):
8787
self.s[i] += val
8888

8989

@@ -101,24 +101,24 @@ class CustomStack:
101101
```java
102102
class CustomStack {
103103
private int[] s;
104-
private int tail;
104+
private int t;
105105

106106
public CustomStack(int maxSize) {
107107
s = new int[maxSize];
108108
}
109-
109+
110110
public void push(int x) {
111-
if (tail < s.length) {
112-
s[tail++] = x;
111+
if (t < s.length) {
112+
s[t++] = x;
113113
}
114114
}
115-
115+
116116
public int pop() {
117-
return tail == 0 ? -1 : s[--tail];
117+
return t == 0 ? -1 : s[--t];
118118
}
119-
119+
120120
public void increment(int k, int val) {
121-
for (int i = 0; i < Math.min(k, tail); ++i) {
121+
for (int i = 0; i < Math.min(k, t); ++i) {
122122
s[i] += val;
123123
}
124124
}
@@ -159,7 +159,6 @@ class CustomStack {
159159
}
160160

161161
increment(k: number, val: number): void {
162-
let tmp: Array<number> = [];
163162
for (let i = Math.max(this.size - k, 0); i < this.size; i++) {
164163
this.stack[i] = this.stack[i] + val;
165164
}
@@ -175,6 +174,84 @@ class CustomStack {
175174
*/
176175
```
177176

177+
### **C++**
178+
179+
```cpp
180+
class CustomStack {
181+
public:
182+
vector<int> s;
183+
int t;
184+
185+
CustomStack(int maxSize) {
186+
s.resize(maxSize);
187+
t = 0;
188+
}
189+
190+
void push(int x) {
191+
if (t < s.size()) s[t++] = x;
192+
}
193+
194+
int pop() {
195+
return t == 0 ? -1 : s[--t];
196+
}
197+
198+
void increment(int k, int val) {
199+
for (int i = 0; i < min(k, t); ++i) s[i] += val;
200+
}
201+
};
202+
203+
/**
204+
* Your CustomStack object will be instantiated and called as such:
205+
* CustomStack* obj = new CustomStack(maxSize);
206+
* obj->push(x);
207+
* int param_2 = obj->pop();
208+
* obj->increment(k,val);
209+
*/
210+
```
211+
212+
### **Go**
213+
214+
```go
215+
type CustomStack struct {
216+
s []int
217+
t int
218+
}
219+
220+
func Constructor(maxSize int) CustomStack {
221+
s := make([]int, maxSize)
222+
return CustomStack{s, 0}
223+
}
224+
225+
func (this *CustomStack) Push(x int) {
226+
if this.t < len(this.s) {
227+
this.s[this.t] = x
228+
this.t++
229+
}
230+
}
231+
232+
func (this *CustomStack) Pop() int {
233+
if this.t == 0 {
234+
return -1
235+
}
236+
this.t--
237+
return this.s[this.t]
238+
}
239+
240+
func (this *CustomStack) Increment(k int, val int) {
241+
for i := 0; i < k && i < this.t; i++ {
242+
this.s[i] += val
243+
}
244+
}
245+
246+
/**
247+
* Your CustomStack object will be instantiated and called as such:
248+
* obj := Constructor(maxSize);
249+
* obj.Push(x);
250+
* param_2 := obj.Pop();
251+
* obj.Increment(k,val);
252+
*/
253+
```
254+
178255
### **...**
179256

180257
```

solution/1300-1399/1381.Design a Stack With Increment Operation/README_EN.md

+95-17
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@ class CustomStack:
6262

6363
def __init__(self, maxSize: int):
6464
self.s = [0] * maxSize
65-
self.tail = 0
65+
self.t = 0
6666

6767
def push(self, x: int) -> None:
68-
if self.tail < len(self.s):
69-
self.s[self.tail] = x
70-
self.tail += 1
68+
if self.t < len(self.s):
69+
self.s[self.t] = x
70+
self.t += 1
7171

7272
def pop(self) -> int:
73-
if self.tail == 0:
73+
if self.t == 0:
7474
return -1
75-
self.tail -= 1
76-
return self.s[self.tail]
75+
self.t -= 1
76+
return self.s[self.t]
7777

7878
def increment(self, k: int, val: int) -> None:
79-
for i in range(min(k, self.tail)):
79+
for i in range(min(k, self.t)):
8080
self.s[i] += val
8181

8282

@@ -92,24 +92,24 @@ class CustomStack:
9292
```java
9393
class CustomStack {
9494
private int[] s;
95-
private int tail;
95+
private int t;
9696

9797
public CustomStack(int maxSize) {
9898
s = new int[maxSize];
9999
}
100-
100+
101101
public void push(int x) {
102-
if (tail < s.length) {
103-
s[tail++] = x;
102+
if (t < s.length) {
103+
s[t++] = x;
104104
}
105105
}
106-
106+
107107
public int pop() {
108-
return tail == 0 ? -1 : s[--tail];
108+
return t == 0 ? -1 : s[--t];
109109
}
110-
110+
111111
public void increment(int k, int val) {
112-
for (int i = 0; i < Math.min(k, tail); ++i) {
112+
for (int i = 0; i < Math.min(k, t); ++i) {
113113
s[i] += val;
114114
}
115115
}
@@ -150,7 +150,6 @@ class CustomStack {
150150
}
151151

152152
increment(k: number, val: number): void {
153-
let tmp: Array<number> = [];
154153
for (let i = Math.max(this.size - k, 0); i < this.size; i++) {
155154
this.stack[i] = this.stack[i] + val;
156155
}
@@ -166,6 +165,85 @@ class CustomStack {
166165
*/
167166
```
168167

168+
### **C++**
169+
170+
```cpp
171+
class CustomStack {
172+
public:
173+
vector<int> s;
174+
int t;
175+
176+
CustomStack(int maxSize) {
177+
s.resize(maxSize);
178+
t = 0;
179+
}
180+
181+
void push(int x) {
182+
if (t < s.size()) s[t++] = x;
183+
}
184+
185+
int pop() {
186+
return t == 0 ? -1 : s[--t];
187+
}
188+
189+
void increment(int k, int val) {
190+
for (int i = 0; i < min(k, t); ++i) s[i] += val;
191+
}
192+
};
193+
194+
/**
195+
* Your CustomStack object will be instantiated and called as such:
196+
* CustomStack* obj = new CustomStack(maxSize);
197+
* obj->push(x);
198+
* int param_2 = obj->pop();
199+
* obj->increment(k,val);
200+
*/
201+
```
202+
203+
### **Go**
204+
205+
```go
206+
type CustomStack struct {
207+
s []int
208+
t int
209+
}
210+
211+
func Constructor(maxSize int) CustomStack {
212+
s := make([]int, maxSize)
213+
return CustomStack{s, 0}
214+
}
215+
216+
func (this *CustomStack) Push(x int) {
217+
if this.t < len(this.s) {
218+
this.s[this.t] = x
219+
this.t++
220+
}
221+
}
222+
223+
func (this *CustomStack) Pop() int {
224+
if this.t == 0 {
225+
return -1
226+
}
227+
this.t--
228+
return this.s[this.t]
229+
}
230+
231+
func (this *CustomStack) Increment(k int, val int) {
232+
for i := 0; i < k && i < this.t; i++ {
233+
this.s[i] += val
234+
}
235+
}
236+
237+
/**
238+
* Your CustomStack object will be instantiated and called as such:
239+
* obj := Constructor(maxSize);
240+
* obj.Push(x);
241+
* param_2 := obj.Pop();
242+
* obj.Increment(k,val);
243+
*/
244+
```
245+
246+
169247
### **...**
170248

171249
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CustomStack {
2+
public:
3+
vector<int> s;
4+
int t;
5+
6+
CustomStack(int maxSize) {
7+
s.resize(maxSize);
8+
t = 0;
9+
}
10+
11+
void push(int x) {
12+
if (t < s.size()) s[t++] = x;
13+
}
14+
15+
int pop() {
16+
return t == 0 ? -1 : s[--t];
17+
}
18+
19+
void increment(int k, int val) {
20+
for (int i = 0; i < min(k, t); ++i) s[i] += val;
21+
}
22+
};
23+
24+
/**
25+
* Your CustomStack object will be instantiated and called as such:
26+
* CustomStack* obj = new CustomStack(maxSize);
27+
* obj->push(x);
28+
* int param_2 = obj->pop();
29+
* obj->increment(k,val);
30+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type CustomStack struct {
2+
s []int
3+
t int
4+
}
5+
6+
func Constructor(maxSize int) CustomStack {
7+
s := make([]int, maxSize)
8+
return CustomStack{s, 0}
9+
}
10+
11+
func (this *CustomStack) Push(x int) {
12+
if this.t < len(this.s) {
13+
this.s[this.t] = x
14+
this.t++
15+
}
16+
}
17+
18+
func (this *CustomStack) Pop() int {
19+
if this.t == 0 {
20+
return -1
21+
}
22+
this.t--
23+
return this.s[this.t]
24+
}
25+
26+
func (this *CustomStack) Increment(k int, val int) {
27+
for i := 0; i < k && i < this.t; i++ {
28+
this.s[i] += val
29+
}
30+
}
31+
32+
/**
33+
* Your CustomStack object will be instantiated and called as such:
34+
* obj := Constructor(maxSize);
35+
* obj.Push(x);
36+
* param_2 := obj.Pop();
37+
* obj.Increment(k,val);
38+
*/

0 commit comments

Comments
 (0)