初级算法之设计问题:shuffle an array

打乱一个没有重复元素的数组。

示例:

// 以数字集合 1, 2 和 3 初始化数组。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
solution.shuffle();

// 重设数组到它的初始状态[1,2,3]。
solution.reset();

// 随机返回数组[1,2,3]打乱后的结果。
solution.shuffle();

关键点在于如何打乱一个数组

    vector<int> preNums;
    Solution(vector<int>& nums) {
        preNums = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return preNums;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
         vector<int> res = preNums;
        for (int i = 0; i < res.size(); ++i) {
            int t = i + rand() % (res.size() - i);
            swap(res[i], res[t]);
        }
        return res;
    }
发布了30 篇原创文章 · 获赞 0 · 访问量 1117

猜你喜欢

转载自blog.csdn.net/qq_41220834/article/details/104056642