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. 阿里巴巴泰山版《Java 开发者手册》,也是一份防坑指南

    我是风筝,公众号「古时的风筝」,一个不只有技术的技术公众号,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者. Spring Cloud 系列文章已经完 ...

  2. FF按钮点击后表单提交

    如果发现<button>提交</button>点击后,所在的表单在ff中自动提交了,则需要添加属性 type='button'! 我也是百度的,记在这里以后方便查看!

  3. 黑马程序员_毕向东_Java基础视频教程——位运算符(随笔)

    位运算符 左移和右移 左移 左移越移越大. 往左移几位就相当于这个数乘于2的几次方 3 << 2 --> 3 * 2^2 = 3 * 4 = 12 3 << 3 --&g ...

  4. 如何搭建一个WEB服务器项目(四)—— 实现安卓端图片加载

    使用Glide安卓图片加载库 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验.先谢谢 ...

  5. HBase Filter 过滤器之 ValueFilter 详解

    前言:本文详细介绍了 HBase ValueFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.ValueFilter 基于列值进行过滤,在工作中涉及到需 ...

  6. 必须使用角色管理工具 安装或配置microsoft.net framework 3.5

    windows server 2008安装sql server 2012后报错,要求安装microsoft.net framework 3.5 sp1 但安装时提示,必须使用角色管理工具 安装或配置m ...

  7. slf4j、log4j、 logback关系详解和相关用法

    slf4j log4j logback关系详解和相关用法 写java也有一段时间了,一直都有用slf4j log4j输出日志的习惯.但是始终都是抱着“拿来主义”的态度,复制粘贴下配置文件就开始编码了, ...

  8. POJ1436

    题目链接:https://vjudge.net/problem/POJ-1436 解题思路:基于y轴建立线段树. 如图是根据样例画出的图.下面都以题目样例为例. 但是,如果仅仅以给出的y1, y2为边 ...

  9. 实用算法系列之RT-Thread链表堆管理器

    [导读] 前文描述了栈的基本概念,本文来聊聊堆是怎么会事儿.RT-Thread 在社区广受欢迎,阅读了其内核代码,实现了堆的管理,代码设计很清晰,可读性很好.故一方面了解RT-Thread内核实现,一 ...

  10. centos 删除文件提示 Operation not permitted

    如果文件上存在 i 标记,那肯定是删不掉的,同样这个文件也不能被编辑.可以进入 root 模式,去除这个标记: root@ubuntu:/home/barret/work# chattr -i 1.m ...