03 2019 档案
leetcode1019
摘要:使用一个栈结构用来记录数据,遍历整个链表,用当前节点的值“依次”与栈内各数字比较。 如果当前值>栈顶值,则栈顶元素进行“标记”,并出栈。一直到栈为空或者当前值<栈内值时,将当前值,入栈。 最后留在栈中的,都“标记”为0。这一步在初始化时就可以完成,因此最后就不用再处理了。 阅读全文
posted @ 2019-03-31 19:37 Sempron2800+ 阅读(224) 评论(0) 推荐(0)
leetcode1018
摘要:根据题目的hint,使用单层循环计算: 阅读全文
posted @ 2019-03-31 14:38 Sempron2800+ 阅读(180) 评论(0) 推荐(0)
leetcode1015
摘要:1 class Solution(object): 2 def smallestRepunitDivByK(self, K: int) -> int: 3 if K % 2 == 0 or K % 5 == 0: 4 return -1 5 r = 0 6 for N in range(1, K + 1): 7 ... 阅读全文
posted @ 2019-03-24 19:14 Sempron2800+ 阅读(176) 评论(0) 推荐(0)
leetcode1016
摘要:1 class Solution(object): 2 def queryString(self, S: str, N: int) -> bool: 3 return all(S.find(bin(i)[2:]) != -1 for i in range(N, N//2 - 1, -1)) 阅读全文
posted @ 2019-03-24 19:11 Sempron2800+ 阅读(120) 评论(0) 推荐(0)
leetcode1014
摘要:这道题暴力算法,会超时: 因此,需要使用动态规划解决: 参考:https://leetcode.com/problems/best-sightseeing-pair/discuss/261041/easy-understand-one-pass-answer-by 阅读全文
posted @ 2019-03-24 15:00 Sempron2800+ 阅读(233) 评论(0) 推荐(0)
leetcode1013
摘要:先判断数组之和是否是3的倍数,如果不是,那么不能三等分。 如果可以,先确定1/3的值,保存在变量target中。 从左向右逐项相加,找到第一组等于target的值的索引,记为i。 从右向左逐项相加,找到第一组等于target的值的索引,记为j。 如果i<j,则可以三等分。 时间复杂度O(n)(三次遍 阅读全文
posted @ 2019-03-24 13:20 Sempron2800+ 阅读(233) 评论(0) 推荐(0)
leetcode1012
摘要:参考:https://leetcode.com/problems/numbers-with-repeated-digits/discuss/256866/Python-O(logN)-solution-with-clear-explanation 以下分析过程是我自己的思路,和上面的代码思路不太一样 阅读全文
posted @ 2019-03-17 19:44 Sempron2800+ 阅读(276) 评论(0) 推荐(0)
leetcode1011
摘要:1 class Solution: 2 def shipWithinDays(self, weights: 'List[int]', D: int) -> int: 3 left = max(weights)#不能小于最重的单个货物 4 right = sum(weights)#不用大于全部货物的总重量 5 while left ... 阅读全文
posted @ 2019-03-17 15:04 Sempron2800+ 阅读(166) 评论(0) 推荐(0)
leetcode1010
摘要:下面这个是简写: 阅读全文
posted @ 2019-03-17 14:15 Sempron2800+ 阅读(121) 评论(0) 推荐(0)
leetcode1009
摘要:1 class Solution: 2 def bitwiseComplement(self, N: int) -> int: 3 if N==0: 4 return 1 5 elif N==1: 6 return 0 7 8 s = list() 9 wh... 阅读全文
posted @ 2019-03-17 13:29 Sempron2800+ 阅读(152) 评论(0) 推荐(0)
leetcode top 100 题目汇总
摘要:首先表达我对leetcode网站的感谢,与高校的OJ系统相比,leetcode上面的题目更贴近工作的需要,而且支持的语言广泛。对于一些比较困难的题目,可以从讨论区中学习别人的思路,这一点很方便。 经过一段时间的练习,我感觉自己的算法水平还是有很大的提升的。与学校开的算法导论的课程相比,我觉得实实在在 阅读全文
posted @ 2019-03-11 09:24 Sempron2800+ 阅读(6116) 评论(0) 推荐(0)
leetcode1008
摘要:比较简单的题目,递归方式构造二叉搜索树。 阅读全文
posted @ 2019-03-10 22:01 Sempron2800+ 阅读(148) 评论(0) 推荐(0)
leetcode1007
摘要:思路,分别记录上部和下部的每一个点数出现的索引,然后对以此判断每一个点数的并集,是否包含了全部的索引。 这样的点数,就是可以满足在同一侧全是一样的点数。 下面就要找最小的移动次数,发现最小的移动次数是保持这个点数出现的次数多的一面不动,而把这个点数出现在另一面的牌进行翻转。 如代码所示:34行,求并 阅读全文
posted @ 2019-03-10 21:30 Sempron2800+ 阅读(136) 评论(0) 推荐(0)
leetcode1006
摘要:这道题的思路就是把运算映射到数组的index上,形成一种规律的运算方式,在循环中进行处理。 阅读全文
posted @ 2019-03-10 20:38 Sempron2800+ 阅读(108) 评论(0) 推荐(0)
leetcode1005
摘要:上面这个一定觉得很啰嗦,那就来个简单的: 思想是一样的,但是简洁了很多。而且由于没有使用新数组保存正和负数,所以空间占用更小。 第一部分代码是思维的过程,第二部分代码是提炼之后的。leetcode上面的题目,大部分都可以用20,30行就搞定。如果代码特别长,那应该是没有抓住问题的本质,这样的代码思维 阅读全文
posted @ 2019-03-10 19:03 Sempron2800+ 阅读(140) 评论(0) 推荐(0)
leetcode218
摘要:神题神解, 参考1:https://leetcode.com/problems/the-skyline-problem/discuss/61194/108-ms-17-lines-body-explained 参考2:https://briangordon.github.io/2014/08/the 阅读全文
posted @ 2019-03-08 16:15 Sempron2800+ 阅读(159) 评论(0) 推荐(0)
leetcode212
摘要:参考:https://leetcode.com/problems/word-search-ii/discuss/59780/Java-15ms-Easiest-Solution-(100.00) 阅读全文
posted @ 2019-03-08 16:07 Sempron2800+ 阅读(207) 评论(0) 推荐(0)
leetcode149
摘要:参考:https://leetcode.com/problems/max-points-on-a-line/discuss/47113/A-java-solution-with-notes 阅读全文
posted @ 2019-03-08 16:02 Sempron2800+ 阅读(242) 评论(0) 推荐(0)
leetcode140
摘要:1 class Solution(object): 2 def wordBreak(self, s, wordDict): 3 """ 4 :type s: str 5 :type wordDict: Set[str] 6 :rtype: List[str] 7 """ 8 r... 阅读全文
posted @ 2019-03-08 16:00 Sempron2800+ 阅读(150) 评论(0) 推荐(0)
leetcode44
摘要:参考:https://leetcode.com/problems/wildcard-matching/discuss/429337/Java%3A-Wild-card! 补充一个python的实现: 思路:动态规划。 举例分析:s='acdcb' p='a*c?b' 。s长度为m,p长度为n。 初始 阅读全文
posted @ 2019-03-08 15:51 Sempron2800+ 阅读(158) 评论(0) 推荐(0)
leetcode315
摘要:从后向前进行判断,线性的搜索时间复杂度比较高,因此建立一个二叉搜索树,每次插入节点的时候,更新父节点的“右侧小”的数字的数量。 参考:https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76587/Eas 阅读全文
posted @ 2019-03-08 11:40 Sempron2800+ 阅读(172) 评论(0) 推荐(0)
leetcode329
摘要:1 public class Solution 2 { 3 bool[,] tags;//用于标记是否已经访问过,false未访问,true已访问 4 int[,] records;//用于标记以当前为起点的最长升序列长度(上下左右四向最长的) 5 6 private int MaxOfFour(int a, in... 阅读全文
posted @ 2019-03-08 10:25 Sempron2800+ 阅读(269) 评论(0) 推荐(0)
leetcode295
摘要:1 public class MedianFinder 2 { 3 List list = null; 4 int count = 0; 5 /** initialize your data structure here. */ 6 public MedianFinder() 7 { 8 ... 阅读全文
posted @ 2019-03-07 23:05 Sempron2800+ 阅读(202) 评论(0) 推荐(0)
leetcode41
摘要:第一次遍历:用一个map记录所有出现过的值,这里忽略掉所有非正数(小于等于0的)。 第二次遍历:从1开始,判断是否在map中,如果在map中继续判断下一个数字,一直到第一个不在字典中的值,则为所求。 17-25行是想写个do while,但是感觉怪怪的。 复杂的算法虽好但是想多了容易掉头发,有时性能 阅读全文
posted @ 2019-03-07 20:08 Sempron2800+ 阅读(164) 评论(0) 推荐(0)
leetcode32
摘要:参考:https://leetcode.com/problems/longest-valid-parentheses/discuss/14126/My-O(n)-solution-using-a-stack 补充一个python的实现: 阅读全文
posted @ 2019-03-07 12:00 Sempron2800+ 阅读(155) 评论(0) 推荐(0)
leetcode312
摘要:参考:https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations 补充一个python的实现: 阅读全文
posted @ 2019-03-07 11:55 Sempron2800+ 阅读(161) 评论(0) 推荐(0)
leetcode10
摘要:参考:https://leetcode.com/problems/regular-expression-matching/discuss/5651/Easy-DP-Java-Solution-with-detailed-Explanation 补充一个python的实现: 本题与剑指Offer 19 阅读全文
posted @ 2019-03-07 11:51 Sempron2800+ 阅读(250) 评论(0) 推荐(0)
leetcode85
摘要:参考:https://leetcode.com/problems/maximal-rectangle/discuss/29055/My-java-solution-based-on-Maximum-Rectangle-in-Histogram-with-explanation 补充一个python的 阅读全文
posted @ 2019-03-07 11:48 Sempron2800+ 阅读(193) 评论(0) 推荐(0)
leetcode124
摘要:参考:https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/39775/Accepted-short-solution-in-Java 阅读全文
posted @ 2019-03-07 11:12 Sempron2800+ 阅读(228) 评论(0) 推荐(0)
leetcode301
摘要:参考:https://leetcode.com/problems/remove-invalid-parentheses/discuss/75027/Easy-Short-Concise-and-Fast-Java-DFS-3-ms-solution 阅读全文
posted @ 2019-03-07 11:01 Sempron2800+ 阅读(179) 评论(0) 推荐(0)
leetcode84
摘要:参考:https://www.geeksforgeeks.org/largest-rectangle-under-histogram/ 补充一个python的实现: 采用了一个小技巧,在heights最后补一个0,则21行到27行的代码就可以省略了。 阅读全文
posted @ 2019-03-06 22:31 Sempron2800+ 阅读(328) 评论(0) 推荐(0)
leetcode76
摘要:1 class Solution: 2 def minWindow(self, s: str, t: str) -> str: 3 n = len(s) 4 if n==0: 5 return "" 6 if len(t)==0: 7 return s 8 #i... 阅读全文
posted @ 2019-03-06 21:45 Sempron2800+ 阅读(240) 评论(0) 推荐(0)
leetcode146
摘要:1 public class LRUCache 2 { 3 int Capacity = 0; 4 int Curlen = 0; 5 long sernumbers; 6 long SerNumbers 7 { 8 get 9 { 10 if (sernumbers <= long.MaxValu 阅读全文
posted @ 2019-03-06 19:26 Sempron2800+ 阅读(186) 评论(0) 推荐(0)
leetcode297
摘要:1 public class Codec 2 { 3 // Encodes a tree to a single string. 4 public string serialize(TreeNode root) 5 { 6 if (root == null) 7 ... 阅读全文
posted @ 2019-03-06 13:35 Sempron2800+ 阅读(343) 评论(0) 推荐(0)
leetcode4
摘要:1 public class Solution 2 { 3 public double FindMedianSortedArrays(int[] nums1, int[] nums2) 4 { 5 var nums = nums1.Concat(nums2).OrderBy(x => x).ToArray(); 6 ... 阅读全文
posted @ 2019-03-06 09:28 Sempron2800+ 阅读(343) 评论(0) 推荐(0)
leetcode23
摘要:投机取巧的办法,时间复杂度还可以,空间占用比较多。 阅读全文
posted @ 2019-03-06 09:13 Sempron2800+ 阅读(216) 评论(0) 推荐(0)
leetcode72
摘要:这是《趣学算法》一书中4-4节提供的代码,在leetcode上运行,速度比较慢,392ms(5.16%),12.9mb(5.22%)。 补充一个python的实现: 240ms,16.4mb 下面提供leetcode中一个12ms,8.5mb的解决方案: 将第二种写法,转化为python语法: 20 阅读全文
posted @ 2019-03-06 08:50 Sempron2800+ 阅读(267) 评论(0) 推荐(0)
leetcode239
摘要:显然这是暴力搜索算法,性能比较差,执行时间1800ms,基本上是超时的边缘了。 下面是参考其他人的,执行时间120ms 思路分析:使用滑动窗口,stack是宽度为k的滑动窗口,并且里面的元素是从大到小排列的。 外层滑动窗口每次取的是stack[0],也就是当前滑动个窗口范围内最大的值的下标,存储在s 阅读全文
posted @ 2019-03-05 22:09 Sempron2800+ 阅读(189) 评论(0) 推荐(0)
leetcode42
摘要:这道题的主要思想是搜索,先找到最大的值maxval,对应的索引maxindex,然后向左右两个方向分别搜索。记height的长度为length。 左侧的部分[0,maxindex),从小到大寻找当前区间的最大值leftmaxval,对应的索引leftmaxindex,然后从计算(leftmax,ma 阅读全文
posted @ 2019-03-05 11:03 Sempron2800+ 阅读(209) 评论(0) 推荐(0)
leetcode128
摘要:1 class Solution: 2 def longestConsecutive(self, nums: 'List[int]') -> int: 3 if len(nums)<=1: 4 return len(nums) 5 6 nums2 = sorted(set(nums)) 7 pre =... 阅读全文
posted @ 2019-03-04 20:03 Sempron2800+ 阅读(182) 评论(0) 推荐(0)
leetcode998
摘要:注意第3行,这里是用构造函数来初始化了一个“成员变量”,这样是可以正常工作的。但是如果使用“类变量”,如下面的第2行,在oj上就会报错。 为了解决这个问题,还可以使用一种方式,就是把结果集合传递到前序遍历的方法中,如下面第2行定义的方法,多了一个prelist参数,用于记录结果: 总结:使用第1种写 阅读全文
posted @ 2019-03-04 16:53 Sempron2800+ 阅读(231) 评论(0) 推荐(0)
leetcode1004
摘要:经过了几次尝试,终于作出来了。主要的思路是滑动窗口: 先记录所有的0的索引,然后选择等K宽的窗口,计算窗口“所连接”的连续1的起止坐标。然后滑动窗口,进行比较,保留最大值。 阅读全文
posted @ 2019-03-04 14:08 Sempron2800+ 阅读(150) 评论(0) 推荐(0)
leetcode1003
摘要:1 class Solution: 2 def isValid(self, S: str) -> bool: 3 n = len(S) 4 if n % 3 != 0: 5 return False 6 while n!=0: 7 i = S.find('abc') 8 ... 阅读全文
posted @ 2019-03-03 19:39 Sempron2800+ 阅读(95) 评论(0) 推荐(0)
leetcode1002
摘要:1 class Solution: 2 def commonChars(self, A: 'List[str]') -> 'List[str]': 3 n = len(A) 4 if n == 1: 5 return A 6 basestr = A[0] 7 baselist = {} ... 阅读全文
posted @ 2019-03-03 13:26 Sempron2800+ 阅读(151) 评论(0) 推荐(0)