problem0528

package
v0.0.0-...-899dd15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 2 Imported by: 0

README

528. Random Pick with Weight

题目

Given an array w of positive integers, where w[i] describes the weight of index i,write a function pickIndexwhich randomlypicks an indexin proportionto its weight.

Note:

  1. 1 <= w.length <= 10000
  2. 1 <= w[i] <= 10^5
  3. pickIndex will be called at most 10000 times.

Example 1:

Input:
["Solution","pickIndex"]
[[[1]],[]]
Output: [null,0]

Example 2:

Input:
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
Output: [null,0,1,1,1,0]

Explanation of Input Syntax:

The input is two lists:the subroutines calledand theirarguments.Solution'sconstructor has one argument, thearray w. pickIndex has no arguments.Argumentsarealways wrapped with a list, even if there aren't any.

解题思路

见程序注释

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Solution

type Solution struct {
	// contains filtered or unexported fields
}

Solution object will be instantiated and called as such: obj := Constructor(w); param_1 := obj.PickIndex();

func Constructor

func Constructor(w []int) Solution

Constructor 返回 Solution

func (*Solution) PickIndex

func (s *Solution) PickIndex() int

PickIndex 根据权重返回 索引号

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL