ShuffleAnArray

2022-12-14 浏览 (663)

ShuffleAnArray.java 源码

package datastructure.array.leetcode;

import java.util.Random;

/**
 * @author roseduan
 * @time 2020/11/16 7:17 下午
 * @description 打乱数组
 */
public class ShuffleAnArray {

    private final int[] nums;
    private final Random random = new Random();

    public ShuffleAnArray(int[] nums) {
        this.nums = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return this.nums;
    }
    
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        int[] data = new int[nums.length];
        System.arraycopy(nums, 0, data, 0, nums.length);

        for (int i = 0; i < data.length; i++) {
            int temp = data[i];
            int r = random.nextInt(data.length);
            data[i] = data[r];
            data[r] = temp;
        }
        return data;
    }
}

你可能感兴趣的文章

BestTimeBuySellStock

BestTimeBuySellStockII

BullsAndCows

  • 所属分类: 后端技术
  • 本文标签: 技术
  • 版权声明: 本文链接 https://seaxiang.com/blog/1cab2d9c904e4fd889a1baa82a1ffbf2