LeetCode 384. Shuffle an Array

来源:互联网 发布:阿里云邮箱网盘升级 编辑:程序博客网 时间:2024/06/07 06:26

描述

按照要求实现函数

解决


class Solution {public:    Solution(vector<int> nums) {        int length = nums.size();        tmp.resize(length);        zz.resize(length);        tmp = nums;        zz = nums;    }    /** Resets the array to its original configuration and return it. */    vector<int> reset() {        return zz;    }    /** Returns a random shuffling of the array. */    vector<int> shuffle() {        int length = tmp.size();        for (int i = 0; i < length; ++i)        {            swap(tmp[i], tmp[rand() % (i + 1)]);        }        return tmp;    }    vector<int> tmp, zz;};/** * 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(); */
0 0