384. Shuffle an Array

c++ random函数:https://www.jb51.net/article/124108.htm

rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数。 这样,如果你要产生0~10的10个整数,可以表达为:

int N = rand() % 11;

这样,N的值就是一个0~10的随机数,如果要产生1~10,则是这样:

总结来说,可以表示为:

a + rand() % n

其中的a是起始值,n是整数的范围。

https://www.cnblogs.com/grandyang/p/5783392.html

Knuth shuffle算法:

https://yjk94.wordpress.com/2017/03/17/%E6%B4%97%E7%89%8C%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF-knuth-shuffle%E7%AE%97%E6%B3%95/

不能直接随机取数字,只能随机取之前的数字才能保证等概率。

注意:取余i+1才能取到为i的余数。遍历到i的时候,必须有交换i自己这一项,所以必须是i+1。

https://leetcode.com/problems/shuffle-an-array/discuss/85965/C++-Knuth-Shuffle-(Fisher-Yates-Shuffle)-Implementation-(352-ms)

class Solution {
public:
Solution(vector<int> nums){
for(auto num : nums)
v.push_back(num);
} /** 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 = rand() % (i + );
swap(res[i], res[t]);
}
return res;
} private:
vector<int> v;
};

leetcode 384. 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. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  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 打乱数组

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

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

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

  9. leetcode mock Shuffle an Array

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

随机推荐

  1. spark HMM

    Scala实现的: https://github.com/skrusche63/spark-intent/tree/master/src/main/scala/de/kp/scala/hmm http ...

  2. P1801 黑匣子[堆]

    题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...

  3. 关于ServletContext的私有方法全局获取返回null问题getServletContext().setAttribute("count", 1)

    1.在Servlet重写了init方法中获取getServletContext()报错提示为空指针 重写了init(ServletConfig)方法,但是重写的init方法内部没有调用super.in ...

  4. Linux 系统 Composer 安装

    Composer 是个包管理工具 在项目中使用它会很方便 本文中用 PHP 安装 1.下载安装 执行命令 curl -sS https://getcomposer.org/installer | ph ...

  5. mysql 端口号被占用

    开始-运行-cmd, 输入 netstat -ano, 看第一列,后面的就是端口,找到3306 ,记住对应的PID!!    然后打开任务管理器 查看 -> 选择列 -> 勾上 PID(进 ...

  6. Django 会议室预定

    表结构分析: from django.db import models # Create your models here. from django.db import models from dja ...

  7. (转载) SQL Server AG集群启动不起来的临时自救大招

    背景 前晚一朋友遇到AG集群发生来回切换不稳定的情况,情急之下,朋友在命令行使用命令重启WSFC集群 结果重启WSFC集群之后,非但没有好转,导致整个AG无法启动,主副本和辅助副本都处于正在解析的状态 ...

  8. RookeyFrame 迁移 线下Model 新增属性 迁移 到数据库

    在类库 Rookey.BusSys.Operate(类库) -> InitOperate.cs(类)  -> App_Start(方法) 添加代码(举例): ToolOperate.Rep ...

  9. 看加载的php.ini 和 phpinfo 配置路径

    php -i | grep "phar.readonly"看当前值php -i | grep "php.ini" 看加载的php.ini是哪个

  10. P1853 投资的最大效益

    题目背景 约翰先生获得了一大笔遗产,他暂时还用不上这一笔钱,他决定进行投资以获得更大的效益.银行工作人员向他提供了多种债券,每一种债券都能在固定的投资后,提供稳定的年利息.当然,每一种债券的投资额是不 ...