IncredibleThings

导航

上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 28 下一页

2018年10月10日 #

LeetCode - Insert Delete GetRandom O(1)

摘要: 用一个 hashmap和一个arraylist, hashmap用来保存元素和其在list中的index, list保存元素。注意remove function中如何用O(1)的方法从list中remove元素。 阅读全文

posted @ 2018-10-10 08:40 IncredibleThings 阅读(91) 评论(0) 推荐(0) 编辑

2018年10月9日 #

LeetCode - Search a 2D Matrix II

摘要: 观察可知越往右下数越大,加上每一行最后一个数是该行最大值,因此可以根据列值减少搜索的列数。定义 i 为矩阵的行,j 为矩阵的列,初始化 i 为0,j 为最后一列。若目标值大于当前matrix [ i ][ j ] 时,说明此行不可能比目标值大,因此 i+1;若小于,则目标值必然不在此列,因此 j-1 阅读全文

posted @ 2018-10-09 09:36 IncredibleThings 阅读(79) 评论(0) 推荐(0) 编辑

LeetCode - Min Stack

摘要: 实现一个stack,要求有push,pop,top功能,还要有一个返回stack最小值的函数(要在常量时间里返回这个值)。在这里我用一个LinkList来实现stack,具体看下面代码。对于最小值,在每次添加或者删除的时候记录即可,这样就能在常量时间获得stack的最小值。 阅读全文

posted @ 2018-10-09 02:39 IncredibleThings 阅读(90) 评论(0) 推荐(0) 编辑

2018年10月8日 #

LeetCode - Baseball Game

摘要: 这道题用stack做,注意stack里面存的元元素是什么。 阅读全文

posted @ 2018-10-08 05:42 IncredibleThings 阅读(68) 评论(0) 推荐(0) 编辑

2018年10月6日 #

LeetCode - Product of Array Except Self

摘要: Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24... 阅读全文

posted @ 2018-10-06 23:51 IncredibleThings 阅读(77) 评论(0) 推荐(0) 编辑

2018年10月5日 #

LeetCode - Best Time to Buy and Sell Stock

摘要: 只需要遍历一次数组,用一个变量记录遍历过数中的最小值,然后每次计算当前值和这个最小值之间的差值最为利润,然后每次选较大的利润来更新。当遍历完成后当前利润即为所求,代码如下: 阅读全文

posted @ 2018-10-05 11:47 IncredibleThings 阅读(102) 评论(0) 推荐(0) 编辑

2018年10月4日 #

LeetCode - Lowest Common Ancestor of a Binary Tree

摘要: The idea is to traverse the tree starting from root. If any of the given keys (n1 and n2) matches with root, then root is LCA (assuming that both keys 阅读全文

posted @ 2018-10-04 10:56 IncredibleThings 阅读(79) 评论(0) 推荐(0) 编辑

2018年10月2日 #

LeetCode - Find All Anagrams in a String

摘要: 这道题先想到了用hash存一下p出现的字符和个数,然后遍历s来做。但超时了: 第一个就是定义的hash数组长度是256,因为ascii码的长度是256位的,所以每一位的索引表示一个字符的计数值。另外就是在窗口的部分,分别用两个指针表示窗口的左边界和右边界。还有用count作为一个计数值,它的含义是窗 阅读全文

posted @ 2018-10-02 19:42 IncredibleThings 阅读(155) 评论(0) 推荐(0) 编辑

LeetCode - Copy List with Random Pointer

摘要: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描list。 第一种方法,就是使用HashMap来坐,HashMap的key存原始pointer,value存新的pointer。 第一遍,先不cop 阅读全文

posted @ 2018-10-02 11:52 IncredibleThings 阅读(77) 评论(0) 推荐(0) 编辑

LeetCode - Serialize and Deserialize Binary Tree

摘要: 无法直接传入int因为它是primitive type, changes made to it will not be visible outside the function scope. 阅读全文

posted @ 2018-10-02 10:09 IncredibleThings 阅读(75) 评论(0) 推荐(0) 编辑

上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 28 下一页