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

这道题让我们给数组洗牌,也就是随机打乱顺序,那么由于之前那道题Linked List Random Node我们接触到了水塘抽样Reservoir Sampling的思想,这道题实际上这道题也是用类似的思路,我们遍历数组每个位置,每次都随机生成一个坐标位置,然后交换当前遍历位置和随机生成的坐标位置的数字,这样如果数组有n个数字,那么我们也随机交换了n组位置,从而达到了洗牌的目的,这里需要注意的是i + rand() % (res.size() - i)不能写成rand() % res.size(),虽然也能通过OJ,但是根据这个帖子的最后部分的概率图表,前面那种写法不是真正的随机分布,应该使用Knuth shuffle算法,感谢热心网友们的留言,参见代码如下:

class Solution {
public:
Solution(vector<int> nums): v(nums) {} /** Resets the array to its original configuration and return it. */
vector<int> reset() {
return v;
} /** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> res = v;
for (int i = ; i < res.size(); ++i) {
int t = i + rand() % (res.size() - i);
swap(res[i], res[t]);
}
return res;
} private:
vector<int> v;
};

类似题目:

Linked List Random Node

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Shuffle an Array 数组洗牌的更多相关文章

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

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

  2. 384. Shuffle an Array数组洗牌

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

  3. 数组洗牌算法-shuffle

    数组洗牌,最近直接的想法是从数组随机取出一个元素,放到另一个数组中,但是这样取出的元素会有重复,必须采取一定的方法保证: 1. 元素不能重复2. 元素被抽取的概率相等,即随机性 数组洗牌经典算法有两种 ...

  4. POJ 3087 Shuffle'm Up(洗牌)

    POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 A common pas ...

  5. Leetcode: Shuffle an Array

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

  6. [LeetCode] 330. Patching Array 数组补丁

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  7. Leetcode(870)-优势洗牌

    给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述. 返回 A 的任意排列,使其相对于 B 的优势最大化. 示例 1: 输入: ...

  8. LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)

    LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...

  9. random array & shuffle 洗牌算法 / 随机算法

    random array & shuffle shuffle 洗牌算法 / 随机算法 https://en.wikipedia.org/wiki/Fisher–Yates_shuffle ES ...

随机推荐

  1. monggodb学习系列:1,mongodb入门

    http://note.youdao.com/share/?id=fa62cd2386f253af68a7e29c6638f158&type=note#/ 放在有道笔记上了,懒得复制过来,有兴 ...

  2. 浅谈Static关键字

    1.使用static关键字声明的属性为全局属性 未使用static关键字指定city之前,如果需要将Tom,Jack,Mary三人的城市均改成Beijing,需要再次声明三次对象的city为Beiji ...

  3. Delphi_06_Delphi_Object_Pascal_基本语法_04

    这一节描述基本语法中的流程语句: 条件语句 IF语句. 选择语句 Case语句.循环语句  while/repeat/for.以及continue.break语句,还有终止程序 运行流程Exit.Ha ...

  4. mysql can't create threads in threadpool

    最近,我们在券商端的mysql运行一段时间后,发生mysql can't create threads in threadpool,如下所示: 据官网一个报告显示,目测是一个bug,内存紧张导致,那天 ...

  5. quartz定时+log4net日志+exchangeservice发邮件

    main using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  6. js picker webapp仿ios picker

    iosselect 在webapp下的一个picker组件 可以轻松实现各类选择器效果.比如地区选择 时间选择 日期选择等. 可以定制依赖关系,可以定制选择层级,可以定制高度 展示项数.无论你是px还 ...

  7. AlloyRenderingEngine继承

    写在前面 不读文章,只对代码感兴趣可以直接跳转到这里 https://github.com/AlloyTeam/AlloyGameEngine然后star一下,多谢支持:). 前几天发了篇向ES6靠齐 ...

  8. 苹果的MDM简介

    MDM(Mobile Device Management)移动设备管理,一般会用于企业管理其移动设备,鉴于iOS是比较封闭的系统很多的功能都难以实现,所以利用苹果的MDM可以达到远程控制设备,像远程定 ...

  9. 用drawRect的方式实现一个尺子

    用drawRect的方式实现了一个尺子选择器,demo在这里:https://github.com/Phelthas/LXMRulerView 效果如图:   如果不考虑复用的问题,我感觉最简单的实现 ...

  10. Android开发学习—— activity

    activity生命周期 #Activity生命周期###void onCreate()* Activity已经被创建完毕###void onStart()* Activity已经显示在屏幕,但没有得 ...