diff options
author | Jack Sangdahl <[email protected]> | 2024-12-30 22:28:47 +0100 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-12-30 22:28:47 +0100 |
commit | e8c6049f79e0102afa0917cca08062232d465650 (patch) | |
tree | 6c23439498b177d662e7f55cfbbdb850e9d4005c /shuffle_array.cpp | |
parent | 00d43ad8605d97544c17cb1775ca884ef0857869 (diff) | |
download | leetcode-e8c6049f79e0102afa0917cca08062232d465650.tar.gz leetcode-e8c6049f79e0102afa0917cca08062232d465650.zip |
384. shuffle an array
Diffstat (limited to 'shuffle_array.cpp')
-rw-r--r-- | shuffle_array.cpp | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/shuffle_array.cpp b/shuffle_array.cpp deleted file mode 100644 index f7dddea..0000000 --- a/shuffle_array.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// 1470. Shuffle the Array -#include "leetcode.h" - -/* - * given array 'nums' consisting of '2n' elements in the form - * [x1, x2, ..., xn, y1, y2, ..., yn] - * return the array in the form [x1, y1, x2, y2, ..., xn, yn] - */ - -class Solution { -public: - vector<int> shuffle(vector<int> &nums, int n) { - int len = nums.size(); - for (int i = n; i < len; i++) - nums[i] = (nums[i] << 10) | nums[i - n]; - int idx = 0; - for (int i = n; i < len; i++, idx += 2) { - nums[idx] = nums[i] & 1023; - nums[idx + 1] = nums[i] >> 10; - } - return nums; - } -}; - -int main() { - Solution obj; - vector<int> nums1 = {2, 5, 1, 3, 4, 7}; - vector<int> nums2 = {1, 2, 3, 4, 4, 3, 2, 1}; - vector<int> nums3 = {1, 1, 2, 2}; - for (auto i : obj.shuffle(nums1, 3)) - cout << i << ' '; // expect: 2,3,5,4,1,7 - cout << endl; - for (auto i : obj.shuffle(nums2, 4)) - cout << i << ' '; // expect: 1,4,2,3,3,2,4,1 - cout << endl; - for (auto i : obj.shuffle(nums3, 2)) - cout << i << ' '; // expect: 1,2,1,2 - cout << endl; -} |