![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
zjj1149
这个作者很懒,什么都没留下…
展开
-
LeetCode 223. Rectangle Area
链接:https://leetcode.com/problems/rectangle-area/思路:显然总覆盖面积等于两矩形面积减去重合部分面积。设两矩阵R1,R2,重合部分某点P(x,y),它必在R1内部,则A <= x <= C,B <= y <= D;必在R2内部,则E <= x <= G,F <= y <= H。综上,max(A, E) ...原创 2019-04-12 14:50:03 · 156 阅读 · 0 评论 -
LeetCode 304. Range Sum Query 2D - Immutable
链接:https://leetcode.com/problems/range-sum-query-2d-immutable/思路矩阵值不变,可以缓存部分和,以减少计算Approach 1: Caching rows. cach[r][c]表示matrix[r][0]~matrix[r][c]总和Approach 2: Caching matrix. cach[r][c]表示matrix[0...原创 2019-04-19 18:46:33 · 109 阅读 · 0 评论 -
LeetCode 201. Bitwise AND of Numbers Range
链接:https://leetcode.com/problems/bitwise-and-of-numbers-range/思路若m = n,结果显然就是m;若m < n,则从m加到n的过程中,最低位一定经历过1和0,AND后最低位为0,rangeBitwiseAnd(m,n) = rangeBitwiseAnd(m>>1, n>>1) << 1。代...原创 2019-04-17 17:32:15 · 136 阅读 · 0 评论 -
LeetCode 240. Search a 2D Matrix II
链接:https://leetcode.com/problems/search-a-2d-matrix-ii/思路设matrix共m行,以matrix[m-1][0]为左下角,matrix[i][j]为右上角划分查找区域。若matrix[i][j] = target,查找成功;若matrix[i][j] > target,则此点正下方的点都大于target,切去这部分,即j–;若matr...原创 2019-04-17 17:06:46 · 81 阅读 · 0 评论 -
LeetCode 1029. Two City Scheduling
链接:https://leetcode.com/problems/two-city-scheduling/思路贪心法。2N个人选N个人去B,则B-A最小的那个肯定去B。证明:设最小的那个人p1,cost分别为A1 B1。在某种分法中,如果它没有去B,选择一个去了B的人p2,设cost为A2 B2。将这两人交换,即改成p1去B,p2去A,则总cost增加-A1+B1-B2+A2=(B1-A1)-...原创 2019-04-21 19:22:22 · 210 阅读 · 0 评论 -
LeetCode 1030. Matrix Cells in Distance Order
链接:https://leetcode.com/problems/matrix-cells-in-distance-order/思路approach 1距离dist最大为R+C,迭代dist,迭代距离为dist的所有点,看点是否在矩阵内,如果dist对应的所有点都不在,那dist不能再加。approach 2点共有R*C个,按照点的距离升序排序,由于RC不大,使用计数排序。先迭代所有点,...原创 2019-04-21 18:23:02 · 195 阅读 · 0 评论 -
LeetCode 1032. Stream of Characters
链接:https://leetcode.com/problems/stream-of-characters/思路query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this ...原创 2019-04-21 17:02:17 · 240 阅读 · 0 评论 -
LeetCode 1031. Maximum Sum of Two Non-Overlapping Subarrays
链接:https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/思路假设L子串在M子串左边。反向迭代A,到达位置i时,计算M子串[i, i+M)总和,并记录到i时已计算的M子串总和的最大值maxMSum[i]。然后正向迭代A,到达位置i时,此时已知的两串总和最大为maximal,计算L子串[i, i+L...原创 2019-04-21 16:29:10 · 325 阅读 · 0 评论 -
LeetCode 169. Majority Element(Boyer-Moore Voting)
链接:https://leetcode.com/problems/majority-element/思路Approach 1: 迭代nums,用map记录各值出现的次数,再迭代map,次数大于size/2的即majority element。Approach 2: Boyer-Moore Voting。基本原理:假设某混合物中,某物质M含量p1,从中分离出一部分,离开部分M的含量p2 <...原创 2019-04-13 17:27:29 · 158 阅读 · 0 评论 -
LeetCode 224. Basic Calculator(infix to postfix)
链接:https://leetcode.com/problems/basic-calculator/,使用的是通用的后缀表达式方法,也可以解答227. Basic Calculator II对表达式求值,本质是为操作符operator(简写opt)找到操作数operand(简写opd)。对于中缀表达式,一个opt的左opd由左部分的某个suffix组成,右opd由右部分的某个prefix组成,当...原创 2019-04-13 12:07:12 · 548 阅读 · 0 评论 -
LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/思路从root到p q分别有一条路径,两条路径从root开始重合,要找到LCA,即找到最后一个重合的结点,找到分叉点。对于路径中处于分叉点以上的任意结点n,p q必然在n的同一个子树中,再加上BST的性质,即可找到分叉点。题目告知所有结...原创 2019-04-16 16:46:38 · 75 阅读 · 0 评论 -
LeetCode 234. Palindrome Linked List(two pointers)
链接:https://leetcode.com/problems/palindrome-linked-list/思路快慢指针找到中间,划分出长n/2的两部分对比两部分。可以在慢指针迭代时就用vector记录前半部分,然后逆序迭代vector,与后半部分依次比较;也可以在慢指针迭代时反转前半链表,然后两部分直接依次比较。这里使用第一种办法,空间复杂度O(n),但写起来更清晰。代码/**...原创 2019-04-16 15:43:59 · 91 阅读 · 0 评论 -
LeetCode 232. Implement Queue using Stacks
链接:https://leetcode.com/problems/implement-queue-using-stacks/思路使用两个栈,stk1用作主要存储。假设已经往stk1里push一些元素,现在要pop,即取栈底的元素,则把上面的元素暂存到stk2里再取栈底。此时把stk2元素放回stk1可恢复原来的顺序,但是,如果下个操作还是pop,此时可以直接取stk2的栈顶。因此,我们不着急恢...原创 2019-04-16 14:36:36 · 185 阅读 · 0 评论 -
LeetCode 225. Implement Stack using Queues
链接:https://leetcode.com/problems/implement-stack-using-queues/思路:用一个队列来存元素,难点在于pop的时候要的是队尾的元素,而队列只能从队首出元素 We use a queue to store elements. The hard point is that when we use pop method, we want to a...原创 2019-04-12 14:49:06 · 114 阅读 · 0 评论 -
LeetCode 307. Range Sum Query - Mutable
链接:https://leetcode.com/problems/range-sum-query-mutable/思路Approach 1类似Immutable的版本,同样记录从下标0~i间元素之和cache,不同在于还要记录原nums,并在update时逐个更新cache。sumRange()复杂度O(1),update()复杂度O(n)。还不如不cache,sumRange()时直接从i...原创 2019-04-20 16:45:40 · 120 阅读 · 0 评论