【LeetCode】抽样 sampling(共4题)
第一部分 水塘抽样 reservoir sampling
水塘抽样的原理:(应该开一篇新文章)pssss
【382】Linked List Random Node (2018年11月15日,新算法)
给了一个单链表,要求等概率的返回单链表的一个结点的值。
解法:我直接随便解了,能过,但是肯定不是面试官想要的XD。先遍历一遍链表求链表长度,然后随机一个出这次是第 x 个结点,(x = rand() % length),然后再遍历到这个结点返回值。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
Solution(ListNode* head) {
ListNode* cur = head;
h1 = head;
for (; cur; cur = cur->next) {
length++;
}
} /** Returns a random node's value. */
int getRandom() {
int node = rand() % length;
auto cur = h1;
for (int i = ; i < node; ++i) {
cur = cur->next;
}
return cur->val;
}
int length = ;
ListNode* h1;
}; /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/
follow-up:What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?
【398】Random Pick Index(2018年11月15日,新算法)
给了一个数组 nums 和一个目标数字 target,要求完成一个设计的题目,等概率的返回 nums 中值为 target 的下标。
题解:我第一次解还是依旧按照按照自己节奏,用了一个 unordered_map<int, vector<int>> 记录数组中值和下标的对应关系。然后在 pick 下标的时候,先把值的下标集合搞出来,然后再随机这个下标值。
class Solution {
public:
Solution(vector<int> nums) {
for (int i = ; i < nums.size(); ++i) {
mp[nums[i]].push_back(i);
}
}
int pick(int target) {
vector<int> vec = mp[target];
int idx = rand() % vec.size();
return vec[idx];
}
unordered_map<int, vector<int>> mp;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
本题用 reservior sampling 应该怎么解待补充。
第二部分 拒绝采样 rejection sampling
【470】Implement Rand10() Using Rand7()
用 rand7() 这个函数生成 rand10() 这个函数的功能。
题解在random专题里面已经写了。orz。 公式是 (randX() - 1) * X + (randX() - 1)
random 专题链接:https://www.cnblogs.com/zhangwanying/p/9964660.html
【478】Generate Random Point in a Circle
【LeetCode】抽样 sampling(共4题)的更多相关文章
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】树(共94题)
[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...
随机推荐
- Test 6.29 T1 预算方案
问题描述 "我需要你为我制订一个购物的方案.我将要为我的宫殿增置一些家具.有n 种备选家具,家具有主件和附件之分.在购买某个主件的附件之前,我必须先购买其对应的主件.某一主件的附件不会是另一 ...
- hdu 6053: TrickGCD (2017 多校第二场 1009) 【莫比乌斯 容斥原理】
题目链接 定义f[n]表示n是最大公约数情况下的计数,F[n]为n是公约数情况下的计数 (可以和 http://www.cnblogs.com/Just--Do--It/p/7197788.html ...
- java 构造方法中super()的作用?
手贱百度了一下 :java里面自定义类的有参构造方法为什么不用super() 举个例子: class Father { Father(){print ('father');}; } class Son ...
- Python3解leetcode Average of Levels in Binary Tree
问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- php中ajax的使用实例讲解
一.总结 1.多复习:代码都挺简单的,就是需要复习,要多看 2.ajax原理:ajax就是部分更新页面,其实还在的html页面监听到事件后,然后传给服务器进行操作,这里用的是get方式来传值到服务器, ...
- leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和]
题目链接 Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square w ...
- [luogu]P1463 [SDOI2005]反素数ant[dfs][数学][数论]
[luogu]P1463 [SDOI2005]反素数ant ——!x^n+y^n=z^n 题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足: ...
- PHP 最全的正则表达式
一.校验数字的表达式 1 数字:^[0-9]*$2 n位的数字:^\d{n}$3 至少n位的数字:^\d{n,}$4 m-n位的数字:^\d{m,n}$5 零和非零开头的数字:^(0|[1-9][0 ...
- C++ 运算符优先级顺序表 (最新/完整)
优先级 运算符 结合律 助记 1 :: 从左至右 作用域 2 a++.a--.type().type{}.a().a[]...-> 从左至右 后缀自增减.函数风格转型.函数调用.下标.成员访问 ...
- Java 设计模式之 简单工厂模式(静态工厂方法模式)
简单工厂模式(Simple Factory Pattern)属于类的创新型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern),是通过专门定义一个类来负责创建其他类的 ...