LeetCode - Algorithms - 1470. Shuffle the Array

I can solve some easy problem on leetcode directly without IDE now. A little bit better.

Problem

1470. Shuffle the Array

Java

1

1
2
3
4
5
6
7
8
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] r = new int[2 * n];
for (int i = 0; i < 2 * n; i++)
r[i] = (i & 1) == 1 ? nums[n + i / 2] : nums[i / 2];
return r;
}
}

Submission Detail

  • 53 / 53 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Shuffle the Array.
  • Memory Usage: 39.3 MB, less than 95.59% of Java online submissions for Shuffle the Array.

2

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] r = new int[2 * n];
for (int i = 0; i < 2 * n; i++) {
if ((i & 1) == 1) {
r[i] = nums[n + i / 2];
} else {
r[i] = nums[i / 2];
}
}
return r;
}
}

Submission Detail

  • 53 / 53 test cases passed.
  • Runtime: 0 ms
  • Memory Usage: 39.7 MB, Your memory usage beats 47.43 % of java submissions.

3

1
2
3
4
5
6
7
8
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] r = new int[2 * n];
for (int i = 0; i < 2 * n; i++)
r[i] = nums[(i & 1) * n + i / 2];
return r;
}
}

Submission Detail

  • 53 / 53 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Shuffle the Array.
  • Memory Usage: 39.6 MB, less than 56.99% of Java online submissions for Shuffle the Array.

4

1
2
3
4
5
6
7
8
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] r = new int[2 * n];
for (int i = 0; i < 2 * n; i++)
r[i] = nums[((i & 1) == 1 ? n : 0) + i / 2];
return r;
}
}

Submission Detail

  • 53 / 53 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Shuffle the Array.
  • Memory Usage: 39.2 MB, less than 95.96% of Java online submissions for Shuffle the Array.