打乱数组问题

Posted Alice_yufeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打乱数组问题相关的知识,希望对你有一定的参考价值。

class Solution 
    private final int[] nums;
    private final int[] shuffle;
    private final Random random;
    public Solution(int[] nums) 
        this.nums = nums;
        this.random = new Random();
        this.shuffle = Arrays.copyOf(nums, nums.length);
    
    public int[] reset() 
        return nums;
    
    public int[] shuffle() 
        for (int i = shuffle.length - 1; i >= 0; --i) 
            int n = random.nextInt(i + 1);
            int t = shuffle[i];
            shuffle[i] = shuffle[n];
            shuffle[n] = t;
        
        return shuffle;
    

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int[] param_1 = obj.reset();
 * int[] param_2 = obj.shuffle();
 */

以上是关于打乱数组问题的主要内容,如果未能解决你的问题,请参考以下文章

如何打乱问题并显示数组的一项

打乱数组问题

Leetcode——打乱数组

题目地址(384. 打乱数组)

《LeetCode之每日一题》:215.打乱数组

如何在 Ruby 中随机排序(打乱)数组?