398. Random Pick Index随机pick函数
[抄题]:
Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.
Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.
Example:
int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums); // pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3); // pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
Random是一个类,要拿来新建对象
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 如果nums[i]不是target,就用continue继续
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 需要新建对象 this.rand = new Random();
[复杂度]:Time complexity: O() Space complexity: O()
[英文数据结构或算法,为什么不用别的数据结构或算法]:
rnd.nextInt(n)
在方法调用返回介于0(含)和n(不含)伪随机,均匀分布的int值,所以括号内的参数必须 >0
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
int[] nums;
Random rand; public Solution(int[] nums) {
this.nums = nums;
this.rand = new Random();
} public int pick(int target) {
//ini: res,
int res = -1, count = 0; //for loop, find or not
for (int i = 0; i < nums.length; i++) {
if (nums[i] != target) continue;
if (rand.nextInt(++count) == 0) res = i;
} //return
return res;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
398. Random Pick Index随机pick函数的更多相关文章
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] 382. Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- random、range和len函数的使用
random.range和len函数的使用 一.random函数 1.random.random()和random.Random(): import random num = random.rando ...
- Java SE基础部分——常用类库之Math和Random类(随机产生数值)
//20160518 Math类常用方法 练习 package MyPackage; public class MathDemo {//定义主类和main方法 public static void m ...
- Oracle随机排序函数和行数字段
随机排序函数dbms_random.value()用法:select * from tablename order by dbms_random.value() 行数字段rownum用法:select ...
- Python基础-random模块及随机生成11位手机号
import random # print(random.random()) # 随机浮点数,默认取0-1,不能指定范围# print(random.randint(1, 20)) # 随机整数,顾头 ...
- MySQL随机字符串函数批量插入数据
简单举个例子: drop table if exists demo1 create table demo1 ( id int primary key auto_increment, name ) ...
随机推荐
- python-unittest-生成测试报告
HTMLTestRunner HTMLTestRunner 是 Python 标准库的 unittest 单元测试框架的一个扩展.它生成易于使用的 HTML 测试报告. 一.目录结构 先来看一下项目的 ...
- Windows Driver Kit Version 7.1.0 ( 也就是 7600.16385.1 ) 下载地址
Windows Driver Kit Version 7.1.0 ( 也就是 7600.16385.1 ) 下载地址 http://download.microsoft.com/download/4/ ...
- GOF23设计模式之状态模式(state)
一.状态模式概述 用于解决系统中复杂对象的状态转换以及不同状态下行为的封装问题. 结构: (1)Context 环境类 环境类中维护一个 State 对象,它定义了当前的状态. (2)State ...
- 自增自减 a++,++a,a--,--a
1.自增(++)自减(--)运算符是一种特殊的算术运算符,在算术运算符中需要两个操作数来进行运算,而自增自减运算符是一个操作数. 实例: public class selfAddMinus{ publ ...
- [Java.web]Web应用结构
以Web应用放在 Tomcat\webapps\ 目录下为例 day01 目录 | |------------- html.jsp.css.js 文件等 |------------- ...
- USB驱动程序之概念介绍学习笔记
现象:把USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有"驱动程序",为何能 ...
- Oracle 10g RAC TAF
Oracle RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会影 ...
- 01——微信小程序官方demo讲解——文件结构
1.环境概览 首先环境配置的部分略过,打开小程序开发工具.选择一个空目录,即可开始一个demo项目. 其中新建成功后的目录如图所示: 2.文件结构描述 如图所示,左边是界面展示,右边是目录结构. 目录 ...
- sql查询语句常用例子
1.查找与jams在同一个单位的员工姓名.性别.部门和职称:select emp_no, emp_name, dept, title from employee where emp_name< ...
- 左侧倒换菜单 frameset 已过时
<!doctype html><html><frameset cols="200,*"> <frame src="left.ht ...