149.Shuffle an Array
来源:互联网 发布:在线ps网站源码 编辑:程序博客网 时间:2024/06/05 19:25
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 likely to be returned.solution.shuffle();// Resets the array back to its original configuration [1,2,3].solution.reset();// Returns the random shuffling of array [1,2,3].solution.shuffle();
Subscribe to see which companies asked this question。
分析:首先把数组的全排列放到一个list中,然后等返回shuffle时就返回list中的随机一个元素。
LinkedList<LinkedList<Integer>> allSet ; //保存全排列 int[] base;//保存初始数组 public Solution(int[] nums) { base = new int[nums.length]; for(int i =0;i<nums.length;i++){ base[i] = nums[i]; } allSet = new LinkedList<LinkedList<Integer>>(); Permutation(nums,0); } /** Resets the array to its original configuration and return it. */ public int[] reset() { return base; } /** Returns a random shuffling of the array. */ public int[] shuffle() { if(allSet.size() <= 1){//如果全排列的个数小于等于1个,则只需要返回base数组即可。 return base; } /*随机生成一个下标*/ Random random = new Random(); int i = random.nextInt(allSet.size());//random.nextInt(20),任意取[0,20)之间整数,其中0可以取到,20取不到 int[] result = new int[base.length]; for(int index = 0;index<base.length;index++){ result[index] = allSet.get(i).get(index); } return result; } /* * nums 表示待排序的字符数组 * beginIndex表示开始全排列的下标 */ public void Permutation(int[] nums, int beginIndex){ int len = nums.length; /* * 说明排序到了最后一个字符,则可以把目前的字符数组转化为字符串加入到最后的结果中 * 这也是终止递归的条件。 */ if (beginIndex == len - 1) { LinkedList<Integer> list = new LinkedList<Integer>(); for(int i=0;i<len;i++){ list.add(nums[i]); } allSet.add(list); return; } /* * 如果当前开始排序的字符不是最后一个字符,则 * 1.把这个字符与其后面的所有字符位置相互换; * 2.然后递归为开始全排列的下标为beginIndex+1; * 3.最后要把第一步中交换的两个字符拿回来,保证charArr还是初始的待排序的字符数组。 * */ else { //index对应的字符表示需要与beginIndex对应的下标交换的字符 /* 1.把这个字符与其后面的所有字符位置相互换;*/ for (int index = beginIndex; index < len;index++) { Swap(nums,beginIndex,index); /*2.然后递归为开始全排列的下标为beginIndex+1;*/ Permutation(nums, beginIndex+1); /*3.最后要把第一步中交换的两个字符拿回来,保证nums还是初始的待排序的字符数组。*/ Swap(nums,beginIndex,index); } } } private void Swap(int[] nums, int i, int j){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }
方法二:依次从后往前,为每个位置找合适的元素。比如当为下标为i的位置找元素时,只需要随机在[0..i]下标中选择一个下标即可。r.nextInt(i+1)随机生成[0,i]之间的整数。
public class Solution { LinkedList<LinkedList<Integer>> allSet ; //保存全排列 int[] base;//保存初始数组 public Solution(int[] nums) { base = new int[nums.length]; for(int i =0;i<nums.length;i++){ base[i] = nums[i]; } } /** Resets the array to its original configuration and return it. */ public int[] reset() { return base; } /** Returns a random shuffling of the array. */ public int[] shuffle() { int len = base.length; int nums[] = new int[len]; for(int i=0;i<len;i++){ nums[i] = base[i]; } Random r = new Random(); /*从后往前,依次为每个位置找元素*/ for(int i= len-1;i>0;i--){ Swap(nums,i,r.nextInt(i+1)); } return nums; } private void Swap(int[] nums, int i, int j){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }
方法三:借助Collections工具方法中的shuffle方法,需要注意的是方法二中采用的从后往前为每个位置找元素借助的就是collections工具中shuffle方法的思想。
public class Solution { int[] base;//保存初始数组 public Solution(int[] nums) { base = new int[nums.length]; for(int i =0;i<nums.length;i++){ base[i] = nums[i]; } } /** Resets the array to its original configuration and return it. */ public int[] reset() { return base; } /** Returns a random shuffling of the array. */ public int[] shuffle() { int len = base.length; int nums[] = new int[len]; ArrayList<Integer> list = new ArrayList<Integer>(len); for(int i=0;i<len;i++){ list.add(base[i]); } //借助collections工具中的shuffle方法 Collections.shuffle(list); for(int i=0;i<len;i++){ nums[i] = list.get(i); } return nums; } }
0 0
- 149.Shuffle an Array
- Shuffle an Array
- 【Leetcode】Shuffle an Array
- Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- LeetCode384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array**
- 384. Shuffle an Array
- 384. Shuffle an Array
- 384. Shuffle an Array
- Myeclipse导入Spring源码后少jar包问题--使用Jar命令重新打包
- hadoop入门之wordcount小案例
- npm install 出错
- android所有布局
- 结合Java反射用简单工厂模式改进抽象工厂模式
- 149.Shuffle an Array
- 【Spark Java API】Transformation(7)—cogroup、join
- 关于OC常用字符串函数介绍
- 集合框架_01_集合框架的构成及分类
- UVALive 6378 Friend Chains (多源最短路 spfa)
- strrev函数
- android 去掉ScrollVIew拉到尽头时再拉的阴影效果
- CAFFE 学习(1). 参考网站和资料
- Codeforces 29C Mail Stamps(离散化 && map初探)