File tree Expand file tree Collapse file tree 5 files changed +171
-0
lines changed
solution/1400-1499/1441.Build an Array With Stack Operations Expand file tree Collapse file tree 5 files changed +171
-0
lines changed Original file line number Diff line number Diff line change @@ -149,6 +149,68 @@ func buildArray(target []int, n int) []string {
149
149
}
150
150
```
151
151
152
+ ### ** C**
153
+
154
+ ``` c
155
+ /* *
156
+ * Note: The returned array must be malloced, assume caller calls free().
157
+ */
158
+ char **buildArray (int * target, int targetSize, int n, int * returnSize) {
159
+ char ** res = (char ** ) malloc(sizeof(char * ) * n * 2);
160
+ int cur = 1;
161
+ int i = 0;
162
+ for (int j = 0; j < targetSize; j++) {
163
+ while (++cur < target[ j] ) {
164
+ res[ i] = (char * ) malloc(sizeof(char) * 8);
165
+ strcpy(res[ i++] , "Push");
166
+ res[ i] = (char * ) malloc(sizeof(char) * 8);
167
+ strcpy(res[ i++] , "Pop");
168
+ }
169
+ res[ i] = (char * ) malloc(sizeof(char) * 8);
170
+ strcpy(res[ i++] , "Push");
171
+ }
172
+ * returnSize = i;
173
+ return res;
174
+ }
175
+ ```
176
+
177
+ ### **TypeScript**
178
+
179
+ ```ts
180
+ function buildArray(target: number[], n: number): string[] {
181
+ const res = [];
182
+ let cur = 0;
183
+ for (const num of target) {
184
+ while (++cur < num) {
185
+ res.push('Push', 'Pop');
186
+ }
187
+ res.push('Push');
188
+ }
189
+ return res;
190
+ }
191
+ ```
192
+
193
+ ### ** Rust**
194
+
195
+ ``` rust
196
+ impl Solution {
197
+ pub fn build_array (target : Vec <i32 >, n : i32 ) -> Vec <String > {
198
+ let mut res = Vec :: new ();
199
+ let mut cur = 1 ;
200
+ for & num in target . iter () {
201
+ while cur < num {
202
+ res . push (" Push" );
203
+ res . push (" Pop" );
204
+ cur += 1 ;
205
+ }
206
+ res . push (" Push" );
207
+ cur += 1 ;
208
+ }
209
+ res . into_iter (). map (String :: from ). collect ()
210
+ }
211
+ }
212
+ ```
213
+
152
214
### ** ...**
153
215
154
216
```
Original file line number Diff line number Diff line change @@ -145,6 +145,68 @@ func buildArray(target []int, n int) []string {
145
145
}
146
146
```
147
147
148
+ ### ** C**
149
+
150
+ ``` c
151
+ /* *
152
+ * Note: The returned array must be malloced, assume caller calls free().
153
+ */
154
+ char **buildArray (int * target, int targetSize, int n, int * returnSize) {
155
+ char ** res = (char ** ) malloc(sizeof(char * ) * n * 2);
156
+ int cur = 1;
157
+ int i = 0;
158
+ for (int j = 0; j < targetSize; j++) {
159
+ while (++cur < target[ j] ) {
160
+ res[ i] = (char * ) malloc(sizeof(char) * 8);
161
+ strcpy(res[ i++] , "Push");
162
+ res[ i] = (char * ) malloc(sizeof(char) * 8);
163
+ strcpy(res[ i++] , "Pop");
164
+ }
165
+ res[ i] = (char * ) malloc(sizeof(char) * 8);
166
+ strcpy(res[ i++] , "Push");
167
+ }
168
+ * returnSize = i;
169
+ return res;
170
+ }
171
+ ```
172
+
173
+ ### **TypeScript**
174
+
175
+ ```ts
176
+ function buildArray(target: number[], n: number): string[] {
177
+ const res = [];
178
+ let cur = 0;
179
+ for (const num of target) {
180
+ while (++cur < num) {
181
+ res.push('Push', 'Pop');
182
+ }
183
+ res.push('Push');
184
+ }
185
+ return res;
186
+ }
187
+ ```
188
+
189
+ ### ** Rust**
190
+
191
+ ``` rust
192
+ impl Solution {
193
+ pub fn build_array (target : Vec <i32 >, n : i32 ) -> Vec <String > {
194
+ let mut res = Vec :: new ();
195
+ let mut cur = 1 ;
196
+ for & num in target . iter () {
197
+ while cur < num {
198
+ res . push (" Push" );
199
+ res . push (" Pop" );
200
+ cur += 1 ;
201
+ }
202
+ res . push (" Push" );
203
+ cur += 1 ;
204
+ }
205
+ res . into_iter (). map (String :: from ). collect ()
206
+ }
207
+ }
208
+ ```
209
+
148
210
### ** ...**
149
211
150
212
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Note: The returned array must be malloced, assume caller calls free().
3
+ */
4
+ char * * buildArray (int * target , int targetSize , int n , int * returnSize ) {
5
+ char * * res = (char * * ) malloc (sizeof (char * ) * n * 2 );
6
+ int cur = 1 ;
7
+ int i = 0 ;
8
+ for (int j = 0 ; j < targetSize ; j ++ ) {
9
+ while (++ cur < target [j ]) {
10
+ res [i ] = (char * ) malloc (sizeof (char ) * 8 );
11
+ strcpy (res [i ++ ], "Push" );
12
+ res [i ] = (char * ) malloc (sizeof (char ) * 8 );
13
+ strcpy (res [i ++ ], "Pop" );
14
+ }
15
+ res [i ] = (char * ) malloc (sizeof (char ) * 8 );
16
+ strcpy (res [i ++ ], "Push" );
17
+ }
18
+ * returnSize = i ;
19
+ return res ;
20
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn build_array ( target : Vec < i32 > , n : i32 ) -> Vec < String > {
3
+ let mut res = Vec :: new ( ) ;
4
+ let mut cur = 1 ;
5
+ for & num in target. iter ( ) {
6
+ while cur < num {
7
+ res. push ( "Push" ) ;
8
+ res. push ( "Pop" ) ;
9
+ cur += 1 ;
10
+ }
11
+ res. push ( "Push" ) ;
12
+ cur += 1 ;
13
+ }
14
+ res. into_iter ( ) . map ( String :: from) . collect ( )
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function buildArray ( target : number [ ] , n : number ) : string [ ] {
2
+ const res = [ ] ;
3
+ let cur = 0 ;
4
+ for ( const num of target ) {
5
+ while ( ++ cur < num ) {
6
+ res . push ( 'Push' , 'Pop' ) ;
7
+ }
8
+ res . push ( 'Push' ) ;
9
+ }
10
+ return res ;
11
+ }
You can’t perform that action at this time.
0 commit comments