From 07e114626716e1d9a5c736c9d7493f45f6e4f619 Mon Sep 17 00:00:00 2001 From: AkifhanIlgaz Date: Mon, 6 Feb 2023 12:58:56 +0300 Subject: [PATCH 1/2] Create: 1470-shuffle-the-array --- rust/1470-shuffle-the-array.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 rust/1470-shuffle-the-array.rs diff --git a/rust/1470-shuffle-the-array.rs b/rust/1470-shuffle-the-array.rs new file mode 100644 index 000000000..cfdb4120e --- /dev/null +++ b/rust/1470-shuffle-the-array.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn shuffle(nums: Vec, n: i32) -> Vec { + let mut nums = nums; + + for i in 0..n as usize { + nums[i] = nums[i] << 10; + nums[i] = nums[i] | nums[i + n as usize]; + } + + let mut j = 2 * n - 1; + + for i in (0..=n as usize - 1).rev() { + let y = nums[i] & (2_i32.pow(10) - 1); + let x = nums[i] >> 10; + nums[j as usize] = y; + nums[j as usize - 1] = x; + j -= 2; + } + + nums + } +} From fa7a99dbd4b1e71317bc17a7c7977d7ab59646ec Mon Sep 17 00:00:00 2001 From: AkifhanIlgaz Date: Mon, 6 Feb 2023 13:01:59 +0300 Subject: [PATCH 2/2] Add 1470-shuffle-the-array --- go/1470-shuffle-the-array.go | 25 +++++++++++++++++++++++++ javascript/1470-shuffle-the-array.js | 23 +++++++++++++++++++++++ typescript/1470-shuffle-the-array.ts | 18 ++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 go/1470-shuffle-the-array.go create mode 100644 javascript/1470-shuffle-the-array.js create mode 100644 typescript/1470-shuffle-the-array.ts diff --git a/go/1470-shuffle-the-array.go b/go/1470-shuffle-the-array.go new file mode 100644 index 000000000..cd3c91ac4 --- /dev/null +++ b/go/1470-shuffle-the-array.go @@ -0,0 +1,25 @@ +package main + +import "math" + +func main() { + +} + +func shuffle(nums []int, n int) []int { + for i := 0; i < n; i++ { + nums[i] = nums[i] << 10 + nums[i] = nums[i] | nums[i+n] + } + + j := 2*n - 1 + + for i := n - 1; i > -1; i-- { + y := nums[i] & (int(math.Pow(2.,10.)) - 1) + x := nums[i] >> 10 + nums[j] = y + nums[j-1] = x + j -= 2 + } + return nums +} \ No newline at end of file diff --git a/javascript/1470-shuffle-the-array.js b/javascript/1470-shuffle-the-array.js new file mode 100644 index 000000000..af377ea1e --- /dev/null +++ b/javascript/1470-shuffle-the-array.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @param {number} n + * @return {number[]} + */ +var shuffle = function (nums, n) { + for (let i = 0; i < n; i++) { + nums[i] = nums[i] << 10; + nums[i] = nums[i] | nums[i + n]; + } + + let j = 2 * n - 1; + + for (let i = n - 1; i > -1; i--) { + let y = nums[i] & (2 ** 10 - 1); + let x = nums[i] >> 10; + nums[j] = y; + nums[j - 1] = x; + j -= 2; + } + + return nums; +}; diff --git a/typescript/1470-shuffle-the-array.ts b/typescript/1470-shuffle-the-array.ts new file mode 100644 index 000000000..fb44d99e3 --- /dev/null +++ b/typescript/1470-shuffle-the-array.ts @@ -0,0 +1,18 @@ +function shuffle(nums: number[], n: number): number[] { + for (let i = 0; i < n; i++) { + nums[i] = nums[i] << 10; + nums[i] = nums[i] | nums[i + n]; + } + + let j = 2 * n - 1; + + for (let i = n - 1; i > -1; i--) { + let y = nums[i] & (2 ** 10 - 1); + let x = nums[i] >> 10; + nums[j] = y; + nums[j - 1] = x; + j -= 2; + } + + return nums; +}