384. Shuffle an 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();
*/
384. Shuffle an Array(java,数组全排列,然后随机取)的更多相关文章
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- leetcode 384. Shuffle an Array
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...
- 384. Shuffle an Array数组洗牌
[抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- LC 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- JAVA数组的遍历和取最值
1.获取数组中的所有元素,会用到数组的遍历 数组的遍历,通常用for循环. public class ArrayDemo { public static void main(String[] args ...
随机推荐
- 外键 Foreign keys
https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...
- Win32汇编学习(1):基本概念
背景知识 Windows 把每一个 Win32 应用程序放到分开的虚拟地址空间中去运行,也就是说每一个应用程序都拥有其相互独立的 4GB 地址空间,当然这倒不是说它们都拥有 4GB 的物理地址空间,而 ...
- windows下使用gvim不支持python3.6问题解决
在用户目录下C:\Users\Administrator\新建vim配置文件夹vimfiles,然后该文件下建立一个文件vimrc vimrc内容: set pythonthreedll=python ...
- vue中使用BetterScroll
BetterScroll可以通过给content加min-height实现永远滚动 content千万不可以删除,千万不要在 content上写v-if
- 解决Linux服务器磁盘空间不足的问题
在linux服务器执行程序时报错: awk: write failure (No space left on device)awk: close failed on file /dev/stdout ...
- _itemmod_day_limit
控制玩家每天获得的物品上限 表说明 `comment` 备注 `entry` 物品 `limitCount`获取上限
- ngui项目花屏问题
项目用的ngui 最近在金立手机上遇到一个问题就是 启动的时候会花一下屏幕 一闪而过 由于ngui默认camera使用的是clear depth 所以按照网上的办法修改 color 跟skybo ...
- P1330 封锁阳光大学
传送门 思路: 依题意可知,在图中的每一条边有且只有一个点被选中(阻止老曹刷街),那么就可以对其采取二分图染色,一条边中:一个点为黑色,另一个点为白色:如果一条边中的两个端点的颜色相同,则说明无解,输 ...
- android 利用CountDownTimer实现时分秒倒计时效果
https://blog.csdn.net/mrzhao_perfectcode/article/details/81289578
- L1-033 出生年
不难,代码: #include <queue> #include <functional> #include <stdio.h> #include <stri ...