题目描述:

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

解题思路:

每次往后读取数组的时候,当读到第i个的时候以1/i的概率随机替换1~i中的任何一个数,这样保证最后每个数字出现在每个位置上的概率都是相等的。

证明:

设x元素在第m次的时候出现在位置i的概率是1/m,那么在第m+1次的时候,x仍然待在位置i的概率是 1/m * m/(m+1) = 1/(m+1)

代码描述:

public class Solution {

	private int[] nums = null;
private Random random = null; public Solution(int[] nums) {
this.nums = nums;
random = new Random();
} /** Resets the array to its original configuration and return it. */
public int[] reset() {
return nums;
} /** Returns a random shuffling of the array. */
public int[] shuffle() {
if(nums == null) return null;
int[] a = (int[])nums.clone();
for(int i = 1; i < nums.length; i++){
int j = random.nextInt(i + 1);
swap(a, i, j);
}
return a;
} private void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[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();
*/

  

Java [Leetcode 384]Shuffle an Array的更多相关文章

  1. leetcode 384. Shuffle an Array

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

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

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

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

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

  4. 384. Shuffle an Array

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

  5. 384. Shuffle an Array数组洗牌

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

  6. LC 384. Shuffle an Array

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

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

    题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...

  8. leetcode mock Shuffle an Array

    1. shuffle算法: http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html 注意:我们一般用的是第二种swap ...

  9. 384 Shuffle an Array 打乱数组

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

随机推荐

  1. Nginx下修改wordpress固定链接后导致访问文章404

    假设我的wordpress博客是的 server{}段是直接放到放到了nginx.conf  (有的人为了方便管理,都习惯在单独写个vhost/目录来存放每个网站的配置文件,这就要根据你自己的设置来添 ...

  2. web.config文件配置解决网站上传大文件限制

    Asp.Net网站对上传文件的大小,请求执行的时间都做了限制,上传的文件如果超过限制或者执行上传时间超出, 文件上传都将失败. 因此,需要配置web.config来增加最大文件上传的大小和执行超时时间 ...

  3. Elasticsearch知识整理

    1:es介绍          Elasticsearch是一个基于Lucene的实时的分布式搜索和分析引擎.设计用于云计算中,          能够达到实时搜索,稳定,可靠,快速,安装使用方便.基 ...

  4. iOS 和Android客户端测试区别整理ing

    区别很多,慢慢发现整理,注重细节,避免遗漏 消息推送区别: 1.推送渠道: 1.1 iOS走iOS自带的渠道进行系统内推送,应用内和应用外推送无明显差别,均可以收到push信息. 1.2 安卓由于谷歌 ...

  5. Codeforces Round #202 (Div. 2)

    第一题水题但是wa了一发,排队记录下收到的25,50,100,看能不能找零,要注意100可以找25*3 复杂度O(n) 第二题贪心,先找出最小的花费,然后就能得出最长的位数,然后循环对每个位上的数看能 ...

  6. 解决Mybatis配置ORM映射 时分秒都为0

    方法一: Date类型的类成员变量使用java.sql.Timestamp 方法二: Mybatis的映射配置javatype=Timestamp.class

  7. OC与JS的交互详解

    事情的起因还是因为项目需求驱动.折腾了两天,由于之前没有UIWebView与JS交互的经历,并且觉得这次在功能上有一定的创造性,特此留下一点文字,方便日后回顾. 我要实现这样一个需求:按照本地的CSS ...

  8. IOS-CALayer(图层)

    BWLayer.m // // BWLayer.m // IOS_0222_CALayer // // Created by ma c on 16/2/23. // Copyright © 2016年 ...

  9. element UI 中DateTimePicker 回传时间选择

    之前在项目中用vue和element,日期和时间选择用的element2.0 的DateTimePicker 日期选择后提交没问题,在编辑页面通过后端返回时间字符串(敲黑板,这里是重点)绑定也没洒问题 ...

  10. h5开发中,利用微信或者QQ登陆以后获取用户头像在canvas画布显示问题

    在实际开发上先的h5页面产品中,总会遇到各种坑,好多坑都是安卓和iPhone端兼容的问题(用电脑谷歌浏览器输入  chrome://inspect/#devices可以用手机USB调试,打开) eg: ...