题目:

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();
*/

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

  1. 384 Shuffle an Array 打乱数组

    打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...

  2. leetcode 384. Shuffle an Array

    384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...

  3. 384. Shuffle an Array数组洗牌

    [抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  4. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  5. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  6. 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

  7. 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  8. LC 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  9. JAVA数组的遍历和取最值

    1.获取数组中的所有元素,会用到数组的遍历 数组的遍历,通常用for循环. public class ArrayDemo { public static void main(String[] args ...

随机推荐

  1. GDOI2018D2T1 谈笑风生

    T1 谈笑风生 [题目描述] [输入] [输出] 一行两个数,所需能量P与在能量最小的前提下最短的到达时间t. [样例输入] 5 7 66 4 3 2 1 5 1 2 1 5 2 3 2 4 2 5 ...

  2. (转) GAN论文整理

    本文转自:http://www.jianshu.com/p/2acb804dd811 GAN论文整理 作者 FinlayLiu 已关注 2016.11.09 13:21 字数 1551 阅读 1263 ...

  3. 《剑指Offer 1.二维数组中的查找》2019-03-25

    剑指Offer  第一题 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数 ...

  4. python运维小技巧

    以下实验均在Linux上进行 1.一秒钟启动一个下载服务器 python版本 python2:  #python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 ...

  5. 在服务器端对sshd做白名单

    1.添加用户 #useradd aaa #passwd aaa -->输入密码:123456 添加3个用户,bbb和ccc与aaa添加一样 2.添加白名单 #vim /etc/ssd/sshd_ ...

  6. C# 获取程序运行目录

    string a = "BaseDirectory:" + AppDomain.CurrentDomain.BaseDirectory + "\r\n" + & ...

  7. 小程序之image图片实现宽度100%,高度自适应

    哇 今天搞了半天  图片一直变形啊啊啊啊 宽度100%   高度给100%   给auto   完全不管用啊啊啊啊 然后最后最终!!!! 首先我们要给我们的图片一个100%的宽度!让它自适应!! .g ...

  8. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    题目链接:Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 第一次写\(dsu\ on\ tree\),来记录一下 \(dsu\ o ...

  9. 【BZOJ】1830: [AHOI2008]Y型项链

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1830 直接枚举目标串是什么,目标串一定是三个字符串的某一个前缀(注意可能为空),然后判断一 ...

  10. JavaSE习题 第八章 线程

    问答题 1.线程和进程是什么关系? 进程是程序的一次动态执行,对应了从代码加载,执行至执行完毕的一个完整的过程 线程是比进程更小的执行单位,一个进程在其执行过程中可以产生多个线程,形成多条执行线索 2 ...