leetcode398
public class Solution {
    int[] nums;
    Random rnd;
    public Solution(int[] nums) {
        this.nums = nums;
        this.rnd = new Random();
    }
    public int pick(int target) {
        int result = -;
        int count = ;
        for (int i = ; i < nums.length; i++) {
            if (nums[i] != target)
                continue;
            if (rnd.nextInt(++count) == )
                result = i;
        }
        return result;
    }
}
/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */
https://leetcode.com/problems/random-pick-index/#/description
leetcode398的更多相关文章
- [Swift]LeetCode398. 随机数索引 | Random Pick Index
		
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
 - leetcode398  and leetcode 382  蓄水池抽样算法
		
382. 链表随机节点 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样. 进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? 示 ...
 
随机推荐
- ACE主动对象模式
			
ACE主动对象模式 ACE主动对象模式解决的核心问题是,异步调用及线程context的切换.ACE主动对象的实现侧重于类代码段的并发访问,这种访问模式仅适合短小的处理流程,比如socket的accep ...
 - iphone动态下拉菜单
			
介绍:实现带动画效果的下拉菜单.用户按下菜单按钮,出现下拉按钮,用户松开菜单按钮,下拉按钮收回. 测试环境:Xcode 4.3, iOS 5.0. 效果图: jQuery特效:http://www.h ...
 - [非原创] 常用加密算法整理 AES/SSL(一)
			
前言: 在伟大的计算机科学家研究下,发明了许多的加密算法,以下做个简答的描述: 一.分类 加密算法分为两种:单向加密.双向加密. 单向加密,不可逆的加密算法,只能加密不能解密: 双向加密,由对称性加密 ...
 - Beautifusoup
			
text = soup.find('div', {'class': 'mulu'}) #查找目录,坑死我了.就这个东西,知乎上看别人写的爬取网络小说,这个最适合我.我一开始老是使用beautifuls ...
 - 19 Python 正则模块和正则表达式
			
什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...
 - bzoj 3681 Arietta
			
一棵有根树,每个点有一个音高,有 $m$ 中弹奏方法,每种方法可以弹奏 $d$ 子树中音高在 $[l,r]$ 间的音符,每种方法最多弹 $t$ 次 求最多能弹出多少个音符 $n \leq 10000$ ...
 - UVALive - 3521 Joseph's Problem (整除分块)
			
给定$n,k$$(1\leqslant n,k\leqslant 10^9)$,计算$\sum\limits _{i=1}^nk\: mod\:i$ 通过观察易发现$k\%i=k-\left \lfl ...
 - HDU4940 Destroy Transportation system(有上下界的最大流)
			
Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...
 - sklearn实践_普通线性回归
			
import pandas as pd import numpy as np import matplotlib.pyplot as plt data = pd.read_csv(r"C:\ ...
 - WPF之X名称空间学习
			
WPF的X名称空间都有什么呢?首先,盗用张图来说明: 我将就图表中的内容进行总结: 1.x:Array具有一个Iteams属性,它能暴漏一个ArratList实例,ArratList实例的内部成员类型 ...