
Algorithm
文章平均质量分 71
tina_tian1
行到水穷处,坐看云起时
展开
-
11. Container With Most Water
文章目录11. Container With Most WaterApproache 1 : DP - Left And Right PointsSummary :1. Consider all combinations of i and j, optimize unnecessary parts (blue).11. Container With Most Waterhttps://lee...原创 2020-03-09 22:46:27 · 189 阅读 · 0 评论 -
384. Shuffle an Array
文章目录384. Shuffle an ArrayApproach 1 : Mark ElementComplexity AnalysisApproach 2 : Fish_YatesProof:Complexity AnalysisSummary1. Random Value2. Re-emphasize: [left-opened right-closed)3. Shuffle Method...原创 2020-02-12 22:24:39 · 137 阅读 · 0 评论 -
382. Linked List Random Node
文章目录382. Linked List Random NodeApproach 1 : Reservoir SamplingProof:Complexity Analysis382. Linked List Random Nodehttps://leetcode.com/problems/linked-list-random-node/Given a singly linked li...原创 2020-02-12 22:21:49 · 140 阅读 · 0 评论 -
215. Kth Largest Element in an Array
文章目录215. Kth Largest Element in an ArrayApproach 1: Priority QueueApproach 2: Quick Select1. Swap low and high elementImprove performance (**16.55%** -> **99.39%**:smile:)2. Repeatly search high a...原创 2020-01-20 13:49:07 · 189 阅读 · 0 评论 -
Linked List Cycle 求链表是否有环及环的入口 证明
一. 问题: 求链表是否有环及环的入口二. leetcode 题目:142.Linked List Cycle IIhttps://leetcode.com/problems/linked-list-cycle-ii/Given a linked list, return the node where the cycle begins. If there is no cycl...原创 2020-01-13 11:40:08 · 238 阅读 · 0 评论 -
KMP算法
感受这是一个能看哭的算法,虽然没几行代码。起初是朋友一起看毛片,看了一个晚上,结果就是苦不堪言,不明觉厉。直到看到coursera上Robert Sedgewick的讲解,觉得神清气爽。后来,因为在看阮一峰的博客,又提到KMP,阮讲得道理好懂,但笼统。就想再对比下Robert教授的讲解,发现自己又傻住了。不得不再花了些时间,理解了一遍,还是记录下理解过程,希望以后不要再...原创 2018-06-28 00:01:24 · 309 阅读 · 0 评论 -
Boyer-Moore算法
1. 算法KMP从前往后搜索,BM txt从前往后,匹配时从后向前,right[R]为最右在pat出现的位置(base index=0).其中Math.max(1, j-*), 是为了避免这种情况,当在pat中坏字符B的位置 小于 right[B]时,避免skip出现负值。 即坏字符B后还有B的情况。如下第二张图所示。2.right[R]的构建3. 算法最...原创 2018-08-15 09:29:10 · 3468 阅读 · 0 评论 -
647. Palindromic Substrings
DescriptionGiven a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even ...原创 2019-09-02 21:15:26 · 173 阅读 · 0 评论 -
1143. Longest Common Subsequence
Given two stringstext1andtext2, return the length of their longest common subsequence.https://leetcode.com/problems/longest-common-subsequence/Solution:class Solution {public: int longe...原创 2019-09-02 21:17:55 · 238 阅读 · 0 评论 -
KMP 算法
一.问题: 查找匹配字符串如从searchText = “fabfafabfab” 中找是否有匹配的字符串 pattern = “fabfab”二.算法: Solution 1: 暴力求解,每次移Solution 2: 快速移动其中next数组中的值为当前字符前的字符串(preSub)的最长的相同前缀后缀的长度,如pattern[5] = b. preSub...原创 2019-09-02 21:17:01 · 137 阅读 · 0 评论 -
Line Segment Intersections
From: https://people.scs.carleton.ca/~michiel/lecturenotes/ALGGEOM/horverintersect.pdf转载 2018-05-17 10:56:28 · 451 阅读 · 0 评论 -
异或应用
又名: 不进位加法给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符a + b = (a ^ b) + (a & b原创 2017-10-19 16:14:32 · 478 阅读 · 0 评论 -
位操作
1. 二分法 a. 条件:low b. 中值:mid = low + (high -low)/2. 数学推理一步可得:mid = (low+high)/2. 虽然看起来效率更高,注意有陷阱,越界。其他: a. 题目看清。b. 小于大于别误写。c. 概念要清,如后段和前段比较,不等时记录前段值,别记后段。原创 2017-04-25 21:38:32 · 214 阅读 · 0 评论 -
1的平方加到n的平方公式, 1的立方加到n的立方公式
证明:利用公式 (n-1)3= n3-3n2+3n-1S3 = 13+23+33+43+...+n3S2 = 12+22+32+42+...+n2S1 = 1 +2 +3 +4+...+n=>S3-3S2+3S1-n = (1-1)3+ (2-1)3+(3-1)3+ (4-1)3+ ... + (n-1)3=S3 -...原创 2017-02-15 15:22:15 · 31473 阅读 · 0 评论 -
算法笔记 - 数组
1. 排序可巧利用数组索引如:Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) f原创 2017-05-02 22:02:26 · 706 阅读 · 0 评论 -
树的遍历 Tree traveral
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * ...原创 2017-04-21 19:36:38 · 680 阅读 · 0 评论 -
随机数
Given a singly linked list, return a random node's value from the linked list. Each node must have thesame probability of being chosen. int getRandom() { int res = head->val; Li原创 2017-05-07 12:54:55 · 204 阅读 · 0 评论 -
Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array.The counts array has the property where counts[i] is the number of smaller elements to the right ofnums[i].Example:原创 2017-05-24 22:04:11 · 233 阅读 · 0 评论 -
Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a原创 2017-05-09 13:27:07 · 204 阅读 · 0 评论 -
红黑树 RBTreee
1. 红黑树是二叉查找树.2. 插入性质:1. 插入新节点总是红色节点2. 如果插入节点的父节点是黑色, 能维持性质3. 如果插入节点的父节点是红色, 破坏了性质. 故插入算法就是通过重新着色或旋转, 来维持性质算法:假设要插入的节点标为N,N的父节点标为P,N的祖父节点标为G,N的叔父节点标为U.1. N = root, N->黑, over。2. P原创 2017-05-17 13:25:52 · 253 阅读 · 0 评论 -
BRTree 删除Practice
原创 2017-05-30 15:40:58 · 397 阅读 · 0 评论 -
树状树组
按照Peter M. Fenwick的说法,正如所有的整数都可以表示成2的幂和,我们也可以把一串序列表示成一系列子序列的和。采用这个想法,我们可将一个前缀和划分成多个子序列的和,而划分的方法与数的2的幂和具有极其相似的方式。一方面,子序列的个数是其二进制表示中1的个数,另一方面,子序列代表的f[i]的个数也是2的幂。三个概念1. Lowest bit number...原创 2017-05-12 16:08:59 · 369 阅读 · 0 评论 -
RSA算法
一. 公钥私钥生成算法步骤1找两个很大的质数p和qn = p * qm = Φ(n) =Φ(p) * Φ(q) = (p -1)(q-1)Note:n的二进制位数即RSA密钥的位数,当前2048ok步骤2. 生成公钥找一个和m互质的整数e(e,n)即为公钥步骤3. 生成私钥找一个整数d, 使得e*d mod m = 1(d,n)即为私钥Note...原创 2016-03-11 16:07:43 · 573 阅读 · 0 评论