Shuffle an Array Solutions in C++
Number 384
Difficulty Medium
Acceptance 52.8%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/shuffle-an-array/// Author : Hao Chen// Date : 2016-08-24class Solution {public:Solution(vector<int> nums) : _nums(nums), _solution(nums) {srand(time(NULL));}/** Resets the array to its original configuration and return it. */vector<int> reset() {return _solution = _nums;}/** Returns a random shuffling of the array. */vector<int> shuffle() {//Fisher Yates//https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffleint i = _solution.size();while ( --i > 0 ) {int j = rand() % (i+1);swap(_solution[i], _solution[j]);}return _solution;}private:vector<int> _nums, _solution;};/*** Your Solution object will be instantiated and called as such:* Solution obj = new Solution(nums);* vector<int> param_1 = obj.reset();* vector<int> param_2 = obj.shuffle();*/
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/shuffle-an-array/description//// Author : liuyubobobo/// Time : 2017-11-10#include <iostream>#include <vector>#include <random>#include <cstdlib>#include <ctime>using namespace std;/// Brute Force Simulation////// Time Complexity: initial: O(n)/// reset: O(1)/// shuffle: O(n)/// Space Complexity: O(n)class Solution {private:vector<int> nums;public:Solution(vector<int> nums) {this->nums.clear();for(int num: nums)this->nums.push_back(num);}/** Resets the array to its original configuration and return it. */vector<int> reset() {return nums;}/** Returns a random shuffling of the array. */vector<int> shuffle() {vector<int> package(nums.begin(), nums.end());vector<int> res;// srand(time(NULL));for(int i = 0 ; i < nums.size() ; i ++){int pos = rand() % package.size();swap(package[pos], package[package.size()-1]);res.push_back(package.back());package.pop_back();}return res;}};void printVec(const vector<int>& vec){for(int num: vec)cout << num << " ";cout << endl;}int main() {int nums[3] = {1, 2, 3};vector<int> vec(nums, nums + 3);Solution obj(vec);printVec(obj.shuffle());printVec(obj.reset());printVec(obj.shuffle());return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/shuffle-an-array/description//// Author : liuyubobobo/// Time : 2017-11-10#include <iostream>#include <vector>#include <random>#include <cstdlib>#include <ctime>using namespace std;/// Fisher Yates Algorithm////// Time Complexity: initial: O(n)/// reset: O(1)/// shuffle: O(n)/// Space Complexity: O(1)class Solution {private:vector<int> nums;public:Solution(vector<int> nums) {this->nums.clear();for(int num: nums)this->nums.push_back(num);}/** Resets the array to its original configuration and return it. */vector<int> reset() {return nums;}/** Returns a random shuffling of the array. */vector<int> shuffle() {vector<int> res(nums.begin(), nums.end());// srand(time(NULL));for(int i = res.size() - 1 ; i >= 0 ; i --){int pos = rand() % (i + 1);swap(res[pos], res[i]);}return res;}};void printVec(const vector<int>& vec){for(int num: vec)cout << num << " ";cout << endl;}int main() {int nums[3] = {1, 2, 3};vector<int> vec(nums, nums + 3);Solution obj(vec);printVec(obj.shuffle());printVec(obj.reset());printVec(obj.shuffle());return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/shuffle-an-array/description//// Author : liuyubobobo/// Time : 2017-11-10#include <iostream>#include <vector>#include <random>#include <cstdlib>#include <ctime>using namespace std;/// Using C++ STL shuffle API////// Time Complexity: initial: O(n)/// reset: O(1)/// shuffle: O(n)/// Space Complexity: O(1)class Solution {private:vector<int> nums;public:Solution(vector<int> nums) {this->nums.clear();for(int num: nums)this->nums.push_back(num);}/** Resets the array to its original configuration and return it. */vector<int> reset() {return nums;}/** Returns a random shuffling of the array. */vector<int> shuffle() {vector<int> res(nums.begin(), nums.end());// srand(time(NULL));random_shuffle(res.begin(), res.end());return res;}};void printVec(const vector<int>& vec){for(int num: vec)cout << num << " ";cout << endl;}int main() {int nums[3] = {1, 2, 3};vector<int> vec(nums, nums + 3);Solution obj(vec);printVec(obj.shuffle());printVec(obj.reset());printVec(obj.shuffle());return 0;}