shuffle-the-array.js (744B)
1 //Shuffle an array where the input is (x1, x2, x3, y1 , y2, y3) 2 //and the output is (x1 , y1, x2, y2 , x3, y3). 3 //The time complexity of my code is O(n) where n is the number 4 //of values in the input array. 5 //Runtime: 66ms Beats: 71.21% 6 //Memory: 44.6MB Beats: 45.7% 7 8 /** 9 * @param {number[]} nums 10 * @param {number} n 11 * @return {number[]} 12 */ 13 var shuffle = function(nums, n) { 14 let x = new Array(n); 15 let y = new Array(n); 16 for(let i = 0 ; i < n ; ++i){ 17 x[i] = nums[i]; 18 } 19 for(let i = 0 ; i < n ; ++i){ 20 y[i] = nums[i + n]; 21 } 22 let count = 0; 23 for(let i = 0 ; i < n * 2; ++i){ 24 nums[i] = x[count]; 25 ++i; 26 nums[i] = y[count]; 27 count += 1; 28 29 } 30 return nums; 31 };