398. 随机数索引

给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。

注意:

数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。

示例:

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[0]等于1。

solution.pick(1);

class Solution {

    int[] numArr;
HashMap<Integer, Integer> hashMap = new HashMap<>(); public Solution(int[] nums) {
this.numArr = nums;
} public int pick(int target) {
int startIndex = hashMap.getOrDefault(target, 0);
int findIndex = -1;
for (int i = startIndex + 1; i < numArr.length; i++) {
if (numArr[i] == target) {
findIndex = i;
break;
}
}
if (findIndex < 0) {
for (int i = 0; i <= startIndex; i++) {
if (numArr[i] == target) {
findIndex = i;
break;
}
}
}
hashMap.put(target,findIndex);
return findIndex;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/

Java实现 LeetCode 398 随机数索引的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  4. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  5. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. Java for LeetCode 153 Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Viterbi-Algorithm(维特比)算法

    CSDN博客:皮乾东  知乎:Htrying  微博:Htring的微博  微信公众号:自然语言处理爱好者(ID:NLP_lover)  文章来自:<数学之美> Viterbi-Algor ...

  2. PDF怎么转换成Word,免费,完整的那种

      简介 PDF可以分为文字型PDF和图片型PDF,文字型PDF即可以选中文字内容的PDF,反之图片型PDF即无法选中文字的PDF,其内容实际上是图片. 本文针对不同类型,介绍PDF转Word方法,可 ...

  3. [hdu5204]水题

    思路:插入的数按指数级增长,所以范围内最多存在logR个数.并且最近i次插入的数,首位置为2^(i-1),且每隔2^i出现一次,于是暴力之..可以用插入排序维护,也可查询时再排下序. 一: #prag ...

  4. EOS基础全家桶(十一)智能合约IDE-EOS_Studio

    简介 我们马上要进入智能合约的开发了,以太坊最初提供了智能合约的功能,并宣告区块链进入2.0时代,而EOS的智能合约更进一步,提供了更多的便利性和可能性.为了进一步了解智能合约,并进行开发,我们需要先 ...

  5. 【基准测试】BenchmarkDotNet介绍

    BenchmarkDotNet 概述 BenchmarkDotNet helps you to transform methods into benchmarks, track their perfo ...

  6. gulp iconfont

    参考如下网站 https://github.com/hjzheng/CUF_meeting_knowledge_share/tree/master/2015-7-24/gulp-test-iconfo ...

  7. JavaDoc文件如何生成

    目录 如何使用 1.通过命令行生成JavaDoc文档 2.IDEA如何配置后生成javadoc文档 javadoc命令是用来生成自己的API文档的 参考信息: @author 作者名 @version ...

  8. 【Java8新特性】关于Java8的Stream API,看这一篇就够了!!

    写在前面 Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一个则是 Stream API(java.util.stream.*) ,那什么是Stream API呢?Java8中的 ...

  9. Spring 注入内部 Beans

    注入内部 Beans inner beans 是在其他 bean 的范围内定义的 bean. 下面是一个基于setter注入的内部 bean 进行配置的配置文件 Beans.xml 文件: <? ...

  10. 存储层技术:JDBC、Hibernate、Mybatis三者之间的比较学习

    JDBC     Hibernate Mybatis    (Java  DataBase Connnection) 是通过JAVA访问数据库 对JDBC的轻量封装 像操作对象操作数据库 对SQL的轻 ...