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. X Error:BadDrawable (individ Pixmap or Window parameter 9)

    #描述 平台:aarch64 系统:ubuntu16.04.02 Qt Version:4.8.7 Qt程序可以正常运行,界面渲染出现问题以及乱码,控制提示错误内容: "unable to ...

  2. SVN 分支代码合并到主线

    SVN 分支代码合并到主线 步骤一安装TortoiseSVN 客户端,在本地 checkout主线代码 步骤二:创建branches分支 步骤三.branches修改提交代码 步骤四:分支代码合并到主 ...

  3. flink基础篇

    Flink面试--核心概念和基础考察 1.简单介绍一下 Flink 2.Flink 相比传统的 Spark Streaming 有什么区别? 3.Flink 的组件栈有哪些?         面试知识 ...

  4. urldecode二次编码

    0x01 <?php if(eregi("hackerDJ",$_GET[id])) { echo("not allowed!"); exit(); } ...

  5. unserialize3

    0x01序列化与反序列化 序列化:将变量转换为可保存或传输的字符串的过程. 反序列化:在适当的的时候把这个字符串再转化成原来的变量使用. 优点: 存储和传输数据更方便,使程序维护性更高. 函数: se ...

  6. wangeditor在移动端的web应用

    废话不多说,直接上代码 前端(前端多说一句,在初始使用阶段,不知道是怎么回事,复制在看云上的文档的配置参数时,一直有错误,后台获取不到$_file,整整一上午,下午上网搜了一下别人的上传图片代码才好用 ...

  7. 【图论算法】LCA最近公共祖先问题

    LCA模板题https://www.luogu.com.cn/problem/P3379题意理解 对于有根树T的两个结点u.v,最近公共祖先LCA(u,v)表示一个结点x,满足x是u.v的祖先且x的深 ...

  8. 用STM32的UART实现DMX512

    写在最前面: DMX512(digital multiplex) 其实就是主机向从机整包单向广播发送的协议(protocol),从机自取所需. 一.链接拓扑(network topology) 根据后 ...

  9. Spring 自动装配 byName

    自动装配 byName,这种模式由属性名称(方法名)指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName.然后,它尝试 ...

  10. poj2823单调队列认知

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 62930   Accepted: 17963 ...