【LeetCode】随机化算法 random(共6题)
【384】Shuffle an Array(2019年3月12日)
Shuffle a set of numbers without duplicates.
实现一个类,里面有两个 api,structure 如下:
class Solution {
public:
Solution(vector<int> nums) {
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
}
};
题解:我们 shuffle 的时候,对于每一个元素 res[i], 都随机出一个 res[j],交换这两个元素就可以了。
class Solution {
public:
Solution(vector<int> nums) {
this->nums = nums;
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return nums;
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
const int n = nums.size();
vector<int> res(nums);
for (int i = ; i < n; ++i) {
int j = rand() % n;
swap(res[i], res[j]);
}
return res;
}
vector<int> nums;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* vector<int> param_1 = obj.reset();
* vector<int> param_2 = obj.shuffle();
*/
【470】Implement Rand10() Using Rand7() (2018年11月15日,新学的算法)(2019年1月23日,算法群复习)
给了一个现成的api rand7(),这个接口能产生 [1,7] 区间的随机数。根据这个api,写一个 rand10() 的算法生成 [1, 10] 区间随机数。
题解:这个题《程序员代码面试指南》上讲了这题。我粗浅的描述一下产生过程:
(1)rand7() 等概率的产生 1,2, 3, 4, 5, 6,7.
(2)rand7()-1 等概率的产生 [0, 6]
(3)(rand7() - 1) * 7 等概率的产生 0, 7, 14, 21, 28, 35, 42
(4)(rand7() - 1) * 7 + (rand7() - 1)等概率的产生 [0, 48] 这49个数字
(5)如果步骤4的结果大于等于40,那么就重复步骤4,直到产生的数小于40.
(6)把步骤5的结果mod 10再加1,就会等概率的随机生成[1, 10].
总之,公式是 (randX() - 1) * X + (randX() - 1)。
// The rand7() API is already defined for you.
// int rand7();
// @return a random integer in the range 1 to 7 class Solution {
public:
int rand10() {
int num = ;
do {
num = (rand7()-) * + (rand7()-);
} while(num >= );
return num % + ;
}
};
本题还有两个follow-up:
What is the expected value for the number of calls to
rand7()function?Could you minimize the number of calls to
rand7()?
《程序员代码面试指南》后面的进阶算法还没看,chp 9, P391
【478】Generate Random Point in a Circle
【497】Random Point in Non-overlapping Rectangles
【519】Random Flip Matrix
【528】Random Pick with Weight (2018年12月31日,昨天算法群 mock 原题)
mock相关链接:https://www.cnblogs.com/zhangwanying/p/10199941.html (第一场-第四题)
Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.
Note:
1 <= w.length <= 100001 <= w[i] <= 10^5pickIndexwill be called at most10000times.
Example 1:
Input:
["Solution","pickIndex"]
[[[1]],[]]
Output: [null,0] Example 2:
Input:
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
Output: [null,0,1,1,1,0]
题解:把 weight 数组求前缀和,然后随机出一个在区间 [0, tot) 中的随机数,然后在前缀和数组中二分判断index。
class Solution {
public:
Solution(vector<int> w) {
const int n = w.size();
vector<int> summ2(n+, );
for (int i = ; i <= n; ++i) {
summ2[i] = w[i-] + summ2[i-];
}
summ = summ2;
}
int pickIndex() {
int tot = summ.back();
int r = (rand() % tot) + ;
auto iter = lower_bound(summ.begin(), summ.end(), r);
int ret = distance(summ.begin(), iter) - ;
return ret;
}
vector<int> summ;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(w);
* int param_1 = obj.pickIndex();
*/
【710】Random Pick with Blacklist
【LeetCode】随机化算法 random(共6题)的更多相关文章
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】树(共94题)
[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】Recursion(共11题)
链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)
题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...
- 【LeetCode算法】LeetCode初级算法——字符串
在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...
随机推荐
- 如何从word文档复制内容到富文本编辑器
在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...
- 有关于log4j详解
Log4j记录日志使用方法 一.什么是log4j Log4J是Apache的一个开放源代码的项目.通过使用Log4J,程序员可以控制日志信息输送的目的地,包括控制台,文件,GUI组件和NT事件记录器, ...
- 循序渐进实现仿QQ界面(二):贴图按钮的三态模拟
开始之前先说一下RingSDK的编译问题,这里演示的程序需要用到最新版本的RingSDK,请务必用SVN到svn://svnhost.cn/RingSDK更新到最新版本,推荐用TortoiseSVN. ...
- windows 系统再重启后,USB口失效(鼠标、U盘都无法识别)的过程及解决方法
今天都差点忘记写随笔.今天在工作中,将电脑重启了一次,悲催了.重启完成后,鼠标无法使用了.最初认为 鼠标的问题,就一直"砸",但后来换了鼠标,仍然不能使用,开始认为没这边简单,拿出 ...
- 洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)
洛谷P1462 通往奥格瑞玛的道路 二分费用. 用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) .二分时需要不断记录合法的 \(mid\) 值. 这里建 ...
- web复制到剪切板js
web复制到剪切板 clipboard.js 好使!开源项目,下载地址: https://github.com/zenorocha/clipboard.js 使用方法: 引入 clipboard.mi ...
- day03—JavaScript中DOM的Event事件方法
转行学开发,代码100天——2018-03-19 1.Event 对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件通常与函数结合使用, ...
- delphi 手势 识别 哈哈
本例尝试在 OnGesture 事件中响应 sgLeft.sgRight 手势; 操作步骤: 1.加 TGestureManager 控件如窗体: GestureManager1; 2.设置窗体属性 ...
- Linux 系统故障修复和修复技巧
我发现Linux系统在启动过程中会出现一些故障,导致系统无法正常启动,我在这里写了几个应用单用户模式.GRUB命令操作.Linux救援模式的故障修复案例帮助大家了解此类问题的解决. 一.单用户模式 L ...
- IIS网站绑定域名
你新建的网站右键-->编辑绑定-->添加 -->类型:http,IP地址:全部未分配,端口号:80,主机名:你的域名,例如yangche.cn-->确定