Java实现 LeetCode 398 随机数索引
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 随机数索引的更多相关文章
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 初探Redis-基础类型Hash
Redis存在五种基础类型:字符串(String).队列(List).哈希(Hash).集合(Set).有序集合(Sorted Set).本次列举出Hash的常用操作. Redis官网:https:/ ...
- 1005 Spell It Right (20分)
1005 Spell It Right (20分) 题目: Given a non-negative integer N, your task is to compute the sum of all ...
- mysql计算
select @csum := 0;select create_time,merchant_id,award as 奖励,total_count as 数量,(@csum := @csum + awa ...
- 开发一个maven脚手架
写在前面 开发新项目就需要搭建新工程,但是搭建新工程的这个过程是非常繁琐浪费时间的,并且不可避免的需要踩坑.更可怕的是,如果是在一个团队中,每新起一个项目都由不同的开发人员去自定义的搭建工程结构,那么 ...
- css实现双色饼图
from:wx--前端早读课 首先回想用css画三角形的方法: <div class="triangle"></div> .triangle { displ ...
- PHP+Apache+MySQL+phpMyAdmin安装和配置
下载网址: PHP:http://windows.php.net/download 版本: php-5.6.31-Win32-VC11-x64.zip Apache:https://www.a ...
- 1.2Go环境搭建之Mac
1.下载mac版go开发工具包,源码包或是安装包都可以 //官方下载地址 https://golang.org/dl/ //下载地址在此 https://dl.google.com/go/go1.11 ...
- day01:判断与循环(20170213)
#1测试判断用户与密码是否正确:import getpassusername = "llz"password = "123455"_username = inp ...
- Form action 方法上传文件
<form method="post" id="form1" runat="server" enctype="multipa ...
- BZOJ1010单调性DP优化
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 10707 Solved: 4445[Submit][S ...