题目:

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. Visual Studio 安装easyX且导入graphics库后,outtextxy提示未定义标示符

    1.点击 “项目” ,然后点击 “属性”. 2. 然后点击左侧 “配置与属性” 下的 “常规” ,在点击 “字符集” ,选择 “使用多字节字符集” 即可解决问题

  2. Shiro学习笔记(一)

    首先展示一下项目的结构目录 工程是用maven创建的   主要是方便管理Jar包  maven的  pom文件中所需要的jar包 <dependencies> <dependency ...

  3. 移动端开发:使用jQuery Mobile还是Zepto

    原:http://blog.csdn.net/liubinwyzbt/article/details/51446771 jQuery Mobile和Zepto是移动端的js库.jQuery Mobil ...

  4. java web 工程更改名字

    如图: 将工程名字struts2Project02更改为struts2Project03,步骤如下: 1. 右键工程名字,选中properties,如图 2.更改项目名字 3.第2步已经真正把项目名字 ...

  5. 烽火HG220G-U E00L2.03M2000光猫改桥接教程

    烽火HG220G-U E00L2.03M2000光猫改桥接教程 P.S. 此教程同样适用于HG221G/HG260G-U/HG261G.(2016.12) 随着北京联通从原有的ONU升级到HGU之后, ...

  6. go 多态

    demo1 // Sample program to show how polymorphic behavior with interfaces. package main import ( &quo ...

  7. Pg MySQL

    https://blog.csdn.net/tiandao2009/article/details/79839037 1架构 2对sql支持的完备性 3join Nest join , 4表分区  p ...

  8. 记录python接口自动化测试--简单总结一下学习过程(第十目)

    至此,从excel文件中循环读取接口到把测试结果写进excel,一个简易的接口自动化测试框架就完成了.大概花了1周的时间,利用下班和周末的时间来理顺思路.编写调试代码,当然现在也还有很多不足,例如没有 ...

  9. Mongodb 分享(一)

    Mongodb使用基础知识: 一.简介 1.mongodb是什么? 1)MongoDB 是一个基于分布式文件存储的数据库.由 )mongodb 客户端:NoSQL Manager for MongoD ...

  10. 放弃 Tightvnc, 选择 Tigervnc

    构建headless vnc server ,我终于放弃了Tightvnc 基于以下原因: 1) 已知的Qt5的键盘映射问题,导致virtualbox 的使用出现困难 https://unix.sta ...