Shuffle an Array (水塘抽样)

随机性问题

水塘抽样算法可保证每个样本被抽到的概率相等

使用场景:从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况

Knuth洗牌算法

拿起第i张牌时,只从它前面的牌随机选出j,或从它后面的牌随机选出j交换即可

 1 class Solution {
 2 public:
 3     Solution(vector<int>& nums) {
 4         v = nums;
 5     }
 6
 7     /** Resets the array to its original configuration and return it. */
 8     vector<int> reset() {
 9         return v;
10     }
11
12     /** Returns a random shuffling of the array. */
13     vector<int> shuffle() {
14         vector<int> res = v;
15         for (int i = 0; i < res.size(); ++i) {
16             int t = i + rand() % (res.size() - i);
17             swap(res[i], res[t]);
18         }
19         return res;
20     }
21     vector<int> v;
22 };
23
24 /**
25  * Your Solution object will be instantiated and called as such:
26  * Solution* obj = new Solution(nums);
27  * vector<int> param_1 = obj->reset();
28  * vector<int> param_2 = obj->shuffle();
29  */

原文地址:https://www.cnblogs.com/demian/p/11240184.html

时间: 2024-08-06 19:32:03

Shuffle an Array (水塘抽样)的相关文章

[LeetCode] Shuffle an Array 数组洗牌

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 lik

Spark MLlib之水塘抽样算法(Reservoir Sampling)

1.理解 问题定义可以简化如下:在不知道文件总行数的情况下,如何从文件中随机的抽取一行? 首先想到的是我们做过类似的题目吗?当然,在知道文件行数的情况下,我们可以很容易的用C运行库的rand函数随机的获得一个行数,从而随机的取出一行,但是,当前的情况是不知道行数,这样如何求呢?我们需要一个概念来帮助我们做出猜想,来使得对每一行取出的概率相等,也即随机.这个概念即蓄水池抽样(Reservoir Sampling). 水塘抽样算法(Reservoir Sampling)思想: 在序列流中取一个数,如

【Leetcode】Shuffle an Array

题目链接:https://leetcode.com/problems/shuffle-an-array/ 题目: 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 retur

Java [Leetcode 384]Shuffle an Array

题目描述: 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 equal

水塘抽样

算法描述: 水塘抽样是一系列的随机算法,其目的在于从包含n个项目的集合S中选取k个样本(从s[n]中选取样本s(k)),其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况. 解决方案: 1.从S[n]中抽取首k项放入「水塘」中对于每一个S[j]项(j ≥ k): for(int i=0;i<k;i++){ R[i] = S[i]; } 2.从0到j的随机产生整数r,若 r < k 则把水塘中的第r项换成S[j]项: for(int j = k+1;j < n;j

384. Shuffle an Array

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 lik

LC 384. Shuffle an Array

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 lik

[Swift]LeetCode384. 打乱数组 | Shuffle an Array

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 lik

leetcode mock Shuffle an Array

1. shuffle算法: http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html 注意:我们一般用的是第二种swap的方法:但是第一种直接选择,然后把最末尾一位填上的方法,也是很好的.只是会需要两倍的空间. 2. Random nextInt(bound)参数表示 0-inclusive,bound-exclusive: [0, bound) package com.company; import java.ut