<Random> 384 398

384. Shuffle an Array

random.nextInt(n) 返回[0, n) 的随机数,故要+1;

class Solution {

    private int[] nums;
    private Random random;

    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 = nums.clone();
        for(int j = 1; j < a.length; j++){
            int i = random.nextInt(j + 1);
            swap(a, i , j);
        }
        return a;
    }

    private void swap(int[] a, int i, int j){
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}

398. Random Pick Index

2 : It‘s probability of selection is 1 * (1/2) * (2/3) = 1/3
3 : It‘s probability of selection is (1/2) * (2/3) = 1/3
4 : It‘s probability of selection is just 1/3

class Solution {

    int[] nums;
    Random random;

    public Solution(int[] nums) {
        this.nums = nums;
        this.random = new Random();
    }

    public int pick(int target) {
        int res = -1;
        int count = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] != target)
                continue;
            if(random.nextInt(++count) == 0)
                res = i;
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/Afei-1123/p/11840861.html

时间: 2024-11-02 18:46:23

<Random> 384 398的相关文章

Reservoir Sampling - 蓄水池抽样算法

蓄水池抽样——<编程珠玑>读书笔记 382. Linked List Random Node 398. Random Pick Index         问题:如何随机从n个对象中选择一个对象,这n个对象是按序排列的,但是在此之前你是不知道n的值的.  思路:如果我们知道n的值,那么问题就可以简单的用一个大随机数rand()%n得到一个确切的随机位置,那么该位置的对象就是所求的对象,选中的概率是1/n. 但现在我们并不知道n的值,这个问题便抽象为蓄水池抽样问题,即从一个包含n个对象的列表S中

[LC] 398. Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note:The array size can be very large. Solution that uses too much extra spa

398 Random Pick Index 随机数索引

给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] nums = new int[] {1,2,3,3,3};Solution solution = new Solution(nums);// pick(3) 应该返回索引 2,3 或者 4.每个索引的返回概率应该相等.solution.pick(3);// pick(1) 应该返回 0.因为只有nums[

398. Random Pick Index

https://leetcode.com/problems/random-pick-index/description/ Reservoir Sampling class Solution { public: vector<int> m; Solution(vector<int> nums) { m = nums; } int pick(int target) { int res = -1; for (int i = 0, cnt = 0; i < m.size(); i++

Random Forest Classification of Mushrooms

There is a plethora of classification algorithms available to people who have a bit of coding experience and a set of data. A common machine learning method is the random forest, which is a good place to start. This is a use case in R of the randomFo

条件随机场介绍(7)—— An Introduction to Conditional Random Fields

参考文献 [1] S.M.AjiandR.J.McEliece,"Thegeneralizeddistributivelaw,"IEEETrans- actions on Information Theory, vol. 46, no. 2, pp. 325–343, 2000. [2] Y.Altun,I.Tsochantaridis,andT.Hofmann,"HiddenMarkovsupportvector machines," in Internation

numpy的random模块

[说明] 翻译自官网的文档. 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909878]]) #random randn(d0, d1, ..., dn) 返回一个样本,具有标准正态分布.

[TypeScript] Create random integers in a given range

Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int between * @param start inclusive * @param before exclusive */ export function randomInt(start: number, before: number) { return start + Math.floor(Math.rand

java中Random随机种子使用

在java中,通过Random生成随机数时,如果设置随机种子,则相同的种子,产生的随机数相同.若不设置则每次随机的不同. Random rnd = new Random(); rnd.setSeed(10);//用于设置种子. rnd.nextInt();// 用于产生随机数. rnd.nextInt(10); // 产生(0-9)数字.