@@ -69,21 +69,21 @@ class CustomStack:
69
69
70
70
def __init__ (self , maxSize : int ):
71
71
self .s = [0 ] * maxSize
72
- self .tail = 0
72
+ self .t = 0
73
73
74
74
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
78
78
79
79
def pop (self ) -> int :
80
- if self .tail == 0 :
80
+ if self .t == 0 :
81
81
return - 1
82
- self .tail -= 1
83
- return self .s[self .tail ]
82
+ self .t -= 1
83
+ return self .s[self .t ]
84
84
85
85
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 )):
87
87
self .s[i] += val
88
88
89
89
@@ -101,24 +101,24 @@ class CustomStack:
101
101
``` java
102
102
class CustomStack {
103
103
private int [] s;
104
- private int tail ;
104
+ private int t ;
105
105
106
106
public CustomStack (int maxSize ) {
107
107
s = new int [maxSize];
108
108
}
109
-
109
+
110
110
public void push (int x ) {
111
- if (tail < s. length) {
112
- s[tail ++ ] = x;
111
+ if (t < s. length) {
112
+ s[t ++ ] = x;
113
113
}
114
114
}
115
-
115
+
116
116
public int pop () {
117
- return tail == 0 ? - 1 : s[-- tail ];
117
+ return t == 0 ? - 1 : s[-- t ];
118
118
}
119
-
119
+
120
120
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) {
122
122
s[i] += val;
123
123
}
124
124
}
@@ -159,7 +159,6 @@ class CustomStack {
159
159
}
160
160
161
161
increment(k : number , val : number ): void {
162
- let tmp: Array <number > = [];
163
162
for (let i = Math .max (this .size - k , 0 ); i < this .size ; i ++ ) {
164
163
this .stack [i ] = this .stack [i ] + val ;
165
164
}
@@ -175,6 +174,84 @@ class CustomStack {
175
174
*/
176
175
```
177
176
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
+
178
255
### ** ...**
179
256
180
257
```
0 commit comments