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. PHP SplQueue 实现队列

    $que = new SplQueue(); $que->enqueue("a");//入队列 $que->enqueue("b"); $que-& ...

  2. Eclips+ADT+SDK构建android开发环境及android自动化测试开发环境

    一. 需要用到的包: 1.adt-bundle-windows-x86_64-20140702.zip+JDK+ant 2.ant下载地址:http://ant.apache.org/bindownl ...

  3. IDEA使用入门设置(从入门到放弃)

      激活 1:设置JDK路径 2:设置maven环境 3:设置tomcat 4:设置快捷键与eclipse相同 4.1:设置字体大小 5:创建maven  java 工程并进行编译打包等操作 6:创建 ...

  4. ubuntu14.04 部署nfs服务

    安装nfs服务 apt-get install nfs-kernel-server 修改配置文件,共享目录为/var/www,*号可替换为客户端IP地址,*默认为任何部署了nfs客户端的IP可以挂载该 ...

  5. Python通过logging记录日志并应用coloredlogs在控制台输出有色字体

    import logging import os from logging.handlers import TimedRotatingFileHandler import coloredlogs # ...

  6. Mac上django 报错 [Errno 13] Permission denied: '/static'

    将setting文件中的 改成:

  7. babyheap_fastbin_attack

    babyheap_fastbin_attack 首先检查程序保护 保护全开.是一个选单系统 分析程序 void new() { int index; // [rsp+0h] [rbp-10h] sig ...

  8. Oracle substr() 字符截取函数

    1.substr函数格式   (俗称:字符截取函数) 格式1: substr(string string, int a, int b); 格式2:substr(string string, int a ...

  9. js遍历删除数组中不符合条件的元素

    //一般解决方法 let arr = [1,2,3]; for(let i=0; i<arr.length; i++){ if(arr[i]==2){ arr.splice(i, 1); i-- ...

  10. php实现大文件上传分片上传断点续传

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...