Skip to content

Commit 0a7d9be

Browse files
committed
feat: add solutions to lc problem: No.1470
No.1470.Shuffle the Array
1 parent 41d4dc1 commit 0a7d9be

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

solution/1400-1499/1470.Shuffle the Array/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,63 @@ func shuffle(nums []int, n int) []int {
126126
}
127127
```
128128

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+
129186
### **...**
130187

131188
```

solution/1400-1499/1470.Shuffle the Array/README_EN.md

+57
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,63 @@ func shuffle(nums []int, n int) []int {
131131
}
132132
```
133133

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+
134191
### **...**
135192

136193
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

0 commit comments

Comments
 (0)