384. Shuffle and Array(java,数组全排列,然后随机取)

题目:

Shuffle a set of numbers without duplicates.

分析:

对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机返回数组的一个排列,

并且使得获得数组每一个排列的概率都是相同的。为此,可以在初始化时,求出数组的所有排列。在使用shuffle方法时,随机返回全排列中的一个。

代码:

public class Solution {

    //存储数组的所有排列
    List<int[]> list = new ArrayList<int[]>();
    public Solution(int[] nums) {
        //首先求所有排列
        permutations(nums,list,0);
    }

    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return list.get(0);
    }

    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        int index = (int)(Math.random() * list.size());
        return list.get(index);
    }
    //求数组的所有排列
    public void permutations(int[] array,List<int[]> list,int start){
        if(array == null){
            return;
        }
        if(start == array.length){
            int[] temp = new int[array.length];
            System.arraycopy(array,0,temp,0,array.length);
            list.add(temp);
        }
        for(int i = start; i < array.length; ++i){
            swap(array,i,start);
            permutations(array,list,start+1);
            swap(array,i,start);
        }
    }
    //交换元素
    public void swap(int[] array,int i,int j){
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

/**
 * 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();
 */
时间: 2024-07-31 11:55:33

384. Shuffle and Array(java,数组全排列,然后随机取)的相关文章

Java [Leetcode 384]Shuffle an Array

题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equal

384. Shuffle an Array

Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally lik

LC 384. Shuffle an Array

Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally lik

nginx-lua取后端redis key为数组时的随机取值.

#按毫秒级取值math.randomseed(tostring(os.time()):reverse():sub(1, 7))假设取服务器ip值为两个,13,24action2={13,24}如果数组元素数大于1if (#action2) > 1 then将服务器ip重新赋值为action2的多个元素值中的一个.action2=action2[math.random(#action2)]打印重新赋值后的aciton2(服务器ip)print (action2)end后续可以自己控制action2

白菜刷LeetCode记-384. Shuffle an Array

今天早上是一道中等难度的题目,考的是洗牌算法. 个人对洗牌算法还是比较不熟悉的,因此是看答案的.参考链接为:https://www.jianshu.com/p/44100741cef5 基本思路为: 1) 将第一个元素与 n 个元素中的任意一个交换: 2) 将第二个与 n - 1 个元素进行交换: 3) 重复上述步骤,直到剩下1个元素. 代码如下: 1 var original; 2 var copy; 3 var num; 4 /** 5 * @param {number[]} nums 6

js【实践】用 js 封装java shuffle函数(打乱数组下标方法)

此方法返回的会是一个全新的数组 所以并不会像java里的shuffle函数一样返回一个引用一样的数组 思路如下: 1.新建一个函数传入需要打乱下标的数组 2.获取数组的长度 3.新建一个用来保存并且返回结果的数组 4.根据数组的长度新建一个随机数(随机数记得要向下取整不然会下标越界) 5.循环添加到结果数组里面 代码如下: <script type="text/javascript"> var testArr = new Array("中国","

java数组和Array类

java数组英文:Arrays 存储相同数值的集合的数据结构 An array is a data structure that stores a collection of values of the same type. You accesseach individual value through an integer index. For example, if a is an array of integers, thena[i] is the ith integer in the a

Java实现字符数组全排列

import org.junit.Test; public class AllSort { public void permutation(char[] buf, int start, int end) { if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可 for (int i = 0; i <= end; i++) { System.out.print(buf[i]); } System.out.println(); } else {//

【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

[215-Kth Largest Element in an Array(数组中第K大的数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth d