File tree 4 files changed +137
-0
lines changed
solution/1400-1499/1470.Shuffle the Array
4 files changed +137
-0
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,63 @@ func shuffle(nums []int, n int) []int {
126
126
}
127
127
```
128
128
129
+ ### ** C**
130
+
131
+ ``` c
132
+ /* *
133
+ * Note: The returned array must be malloced, assume caller calls free().
134
+ */
135
+ int *shuffle (int * nums, int numsSize, int n, int * returnSize) {
136
+ int * res = (int * ) malloc(sizeof(int) * n * 2);
137
+ for (int i = 0; i < n; i++) {
138
+ res[ 2 * i] = nums[ i] ;
139
+ res[ 2 * i + 1] = nums[ i + n] ;
140
+ }
141
+ * returnSize = n * 2;
142
+ return res;
143
+ }
144
+ ```
145
+
146
+ ### **Rust**
147
+
148
+ ```rust
149
+ impl Solution {
150
+ pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
151
+ let n = n as usize;
152
+ let mut res = Vec::new();
153
+ for i in 0..n {
154
+ res.push(nums[i]);
155
+ res.push(nums[n + i]);
156
+ }
157
+ res
158
+ }
159
+ }
160
+ ```
161
+
162
+ ``` rust
163
+ impl Solution {
164
+ pub fn shuffle (mut nums : Vec <i32 >, n : i32 ) -> Vec <i32 > {
165
+ let n = n as usize ;
166
+ for i in 0 .. n * 2 {
167
+ let mut j = i ;
168
+ while nums [i ] > 0 {
169
+ j = if j < n {
170
+ 2 * j
171
+ } else {
172
+ 2 * (j - n ) + 1
173
+ };
174
+ nums . swap (i , j );
175
+ nums [j ] *= - 1 ;
176
+ }
177
+ }
178
+ for i in 0 .. n * 2 {
179
+ nums [i ] *= - 1 ;
180
+ }
181
+ nums
182
+ }
183
+ }
184
+ ```
185
+
129
186
### ** ...**
130
187
131
188
```
Original file line number Diff line number Diff line change @@ -131,6 +131,63 @@ func shuffle(nums []int, n int) []int {
131
131
}
132
132
```
133
133
134
+ ### ** C**
135
+
136
+ ``` c
137
+ /* *
138
+ * Note: The returned array must be malloced, assume caller calls free().
139
+ */
140
+ int *shuffle (int * nums, int numsSize, int n, int * returnSize) {
141
+ int * res = (int * ) malloc(sizeof(int) * n * 2);
142
+ for (int i = 0; i < n; i++) {
143
+ res[ 2 * i] = nums[ i] ;
144
+ res[ 2 * i + 1] = nums[ i + n] ;
145
+ }
146
+ * returnSize = n * 2;
147
+ return res;
148
+ }
149
+ ```
150
+
151
+ ### **Rust**
152
+
153
+ ```rust
154
+ impl Solution {
155
+ pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
156
+ let n = n as usize;
157
+ let mut res = Vec::new();
158
+ for i in 0..n {
159
+ res.push(nums[i]);
160
+ res.push(nums[n + i]);
161
+ }
162
+ res
163
+ }
164
+ }
165
+ ```
166
+
167
+ ``` rust
168
+ impl Solution {
169
+ pub fn shuffle (mut nums : Vec <i32 >, n : i32 ) -> Vec <i32 > {
170
+ let n = n as usize ;
171
+ for i in 0 .. n * 2 {
172
+ let mut j = i ;
173
+ while nums [i ] > 0 {
174
+ j = if j < n {
175
+ 2 * j
176
+ } else {
177
+ 2 * (j - n ) + 1
178
+ };
179
+ nums . swap (i , j );
180
+ nums [j ] *= - 1 ;
181
+ }
182
+ }
183
+ for i in 0 .. n * 2 {
184
+ nums [i ] *= - 1 ;
185
+ }
186
+ nums
187
+ }
188
+ }
189
+ ```
190
+
134
191
### ** ...**
135
192
136
193
```
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
+ int * shuffle (int * nums , int numsSize , int n , int * returnSize ) {
5
+ int * res = (int * ) malloc (sizeof (int ) * n * 2 );
6
+ for (int i = 0 ; i < n ; i ++ ) {
7
+ res [2 * i ] = nums [i ];
8
+ res [2 * i + 1 ] = nums [i + n ];
9
+ }
10
+ * returnSize = n * 2 ;
11
+ return res ;
12
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn shuffle ( nums : Vec < i32 > , n : i32 ) -> Vec < i32 > {
3
+ let n = n as usize ;
4
+ let mut res = Vec :: new ( ) ;
5
+ for i in 0 ..n {
6
+ res. push ( nums[ i] ) ;
7
+ res. push ( nums[ n + i] ) ;
8
+ }
9
+ res
10
+ }
11
+ }
You can’t perform that action at this time.
0 commit comments