We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 055b807 commit 9b97974Copy full SHA for 9b97974
C++/shuffle-the-array.cpp
@@ -4,22 +4,18 @@
4
class Solution {
5
public:
6
vector<int> shuffle(vector<int>& nums, int n) {
7
- static const auto& dest = [](int i, int n) {
+ static const auto& index = [&n](int i) {
8
return (i < n) ? 2 * i : 2 * (i - n) + 1;
9
};
10
for (int i = 0; i < nums.size(); ++i) {
11
- if (nums[i] < 0) {
12
- continue;
13
- }
14
int j = i;
15
- do {
16
- j = dest(j, n);
17
- swap(nums[i], nums[j]);
18
- nums[j] = -nums[j];
19
- } while (j != i);
+ while (nums[i] >= 0) {
+ j = index(j);
+ tie(nums[i], nums[j]) = pair(nums[j], ~nums[i]);
+ }
20
}
21
for (auto& num : nums) {
22
- num = -num;
+ num = ~num;
23
24
return nums;
25
0 commit comments