
leetcode
xyqzki
wahahahaahahahahah
展开
-
leetcode -- Shortest Palindrome -- 不太理解
https://leetcode.com/problems/shortest-palindrome/solution: http://bookshadow.com/weblog/2015/05/22/leetcode-shortest-palindrome/原创 2015-12-08 20:44:01 · 451 阅读 · 0 评论 -
leetcode -- Integer to English Words -- 无聊题目,很麻烦,再做一遍
https://leetcode.com/problems/integer-to-english-words/自己的code,都没有通过。还是判断>99, > 19, >0作为条件。参考http://bookshadow.com/weblog/2015/08/31/leetcode-integer-english-words/原创 2015-12-09 17:08:48 · 448 阅读 · 0 评论 -
leetcode -- Same Tree -- 简单重点
https://leetcode.com/problems/same-tree/我的方法是用dfs来做my code:class Solution(object): def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool原创 2015-12-08 21:34:43 · 472 阅读 · 0 评论 -
leetcode -- Climbing Stairs -- 简单重要
https://leetcode.com/problems/climbing-stairs/简单的dp思路 d[n] = d[n - 1] + d[n - 2]class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """原创 2015-12-08 23:16:22 · 391 阅读 · 0 评论 -
leetcode -- Remove Duplicates from Sorted Array -- 简单重点
https://leetcode.com/problems/remove-duplicates-from-sorted-array/思路简单。但是要找对循环点。removeDuplicates2中用j scan所有元素才是最佳方案。 ref: http://www.cnblogs.com/zuoyuan/p/3779816.htmlclass Solution(object): def rem原创 2015-12-09 11:23:25 · 549 阅读 · 0 评论 -
leetcode -- Swap Nodes in Pairs -- 简单重点
https://leetcode.com/problems/swap-nodes-in-pairs/没什么技巧。注意不要出错。交换node的操作要仔细。class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode原创 2015-12-09 22:05:00 · 617 阅读 · 0 评论 -
leetcode -- Longest Common Prefix -- 简单O(n^2)就行
https://leetcode.com/problems/longest-common-prefix/class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if原创 2015-12-08 21:05:24 · 440 阅读 · 0 评论 -
leetcode -- Excel Sheet Column Title -- 简单要看
https://leetcode.com/problems/excel-sheet-column-title/similar question https://leetcode.com/problems/excel-sheet-column-number/使用chr()以及ord()class Solution(object): def convertToTitle(self, n):原创 2015-12-08 23:42:50 · 505 阅读 · 0 评论 -
leetcode -- Maximum Depth of Binary Tree -- 重点
https://leetcode.com/problems/maximum-depth-of-binary-tree/recursive的方法最简单class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """原创 2015-12-09 00:16:26 · 452 阅读 · 0 评论 -
leetcode -- Valid Parentheses -- 简单重点
https://leetcode.com/problems/valid-parentheses/要用到stack。 参考http://www.cnblogs.com/zuoyuan/p/3779772.html正确code:class Solution: # @return a boolean def isValid(self, s): stack = []原创 2015-12-09 12:15:54 · 473 阅读 · 0 评论 -
leetcode -- Sort List
https://leetcode.com/problems/sort-list/这里又复习了一遍merge list的操作。用归并排序就行参考http://www.cnblogs.com/zuoyuan/p/3699508.htmlclass Solution(object): def mergeList(self, head1, head2): if not head1:原创 2015-12-09 19:56:27 · 346 阅读 · 0 评论 -
leetcode -- Rotate List -- 重点
https://leetcode.com/problems/rotate-list/这里思路很简单。但是要注意,k可以大于len(linkedlist). 要求得长度之后取mod。还有就是这个rotate,不需要把rotate部分逆置。 def rotateRight(self, head, k): """ :type head: ListNode原创 2015-12-09 21:46:21 · 364 阅读 · 0 评论 -
leetcode -- Majority Element -- 简单,但是还有很多其他方法
https://leetcode.com/problems/majority-element/import mathclass Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """原创 2015-12-08 21:12:52 · 500 阅读 · 0 评论 -
leetcode -- Count and Say -- 理解题意
https://leetcode.com/problems/count-and-say/重点在理解题意。count and sayclass Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ if n == 1原创 2015-12-09 11:45:44 · 808 阅读 · 0 评论 -
leetcode -- Linked List Cycle -- 重点
https://leetcode.com/problems/linked-list-cycle/参考http://www.cnblogs.com/zuoyuan/p/3701639.html一开始我错误的code:class Solution(object): def hasCycle(self, head): """ :type head: ListNode原创 2015-12-09 20:46:36 · 338 阅读 · 0 评论 -
leetcode -- Linked List Cycle II -- 重点
https://leetcode.com/problems/linked-list-cycle-ii/参考http://www.cnblogs.com/zuoyuan/p/3701877.html 有原理解释我的codeclass Solution(object): def detectCycle(self, head): """ :type head: Li原创 2015-12-09 22:53:12 · 348 阅读 · 0 评论 -
leetcode -- Flatten Binary Tree to Linked List -- 重点难点
class Solution(object): def flatten(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ if not root:原创 2015-12-10 00:00:08 · 429 阅读 · 0 评论 -
leetcode -- Partition List -- 常见题型重点
https://leetcode.com/problems/partition-list/“穿针引线”法。 构造两个dummy,然后穿针引线。参考http://www.cnblogs.com/zuoyuan/p/3783276.htmlclass Solution(object): def partition(self, head, x): """ :type原创 2015-12-10 00:40:18 · 410 阅读 · 0 评论 -
leetcode -- Reverse Words in a String -- 太简单,忽略
https://leetcode.com/problems/reverse-words-in-a-string/class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ return " ".join(s原创 2015-12-10 13:16:31 · 483 阅读 · 0 评论 -
leetcode -- Maximal Square -- DP重点
https://leetcode.com/problems/maximal-square/对于DP的问题,要写state equation, 这里state选择 square length rather than the required area. 这是2D的dp,所以应该是dp[x][y] =?.这里dp[x][y]表示以(x,y)为右下角点的最大square的边长。这里有点不一样的就是,普通原创 2015-12-10 18:07:05 · 549 阅读 · 0 评论 -
leetcode -- Remove Duplicates from Sorted List II -- 简单重点
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/two pointers链表操作就行class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rty原创 2015-12-11 16:35:46 · 510 阅读 · 0 评论 -
leetcode -- Best Time to Buy and Sell Stock II -- 重点注意思路
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/因为可以进行无限次交易,并且在下一次buy之前必须已经sell。所以只需要把所有price曲线价格上涨的部分加起来就行。参考 http://www.cnblogs.com/zuoyuan/p/3765980.htmlclass Solution(object):原创 2015-12-10 22:44:24 · 476 阅读 · 0 评论 -
leetcode -- Convert Sorted Array/List to Binary Search Tree -- 简单重点
如果是sorted arrayhttps://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 参考 http://www.cnblogs.com/zuoyuan/p/3722103.html 找到中点,对左右递归就行class Solution(object): def sortedArrayToBST原创 2015-12-11 14:24:29 · 553 阅读 · 0 评论 -
leetcode -- Merge k Sorted Lists -- 重点有trick
https://leetcode.com/problems/merge-k-sorted-lists/思路就是把所有的head节点入heap,然后把root,heappop出来。再把被pop出来的node所属于的list的下一个node入heap. 知道heap为空这里要用到heap,python中有heapq.heapify([])这个函数可以对heap进行create,从而进行操作。以及 hea原创 2015-12-11 16:01:56 · 1009 阅读 · 0 评论 -
leetcode -- Product of Array Except Self -- 重点。常考
https://leetcode.com/problems/product-of-array-except-self/由于output[i] = (x0 * x1 * … * xi-1) * (xi+1 * …. * xn-1)因此执行两趟循环:第一趟正向遍历数组,计算x0x_0 ~ xi−1x_{i-1}的乘积第二趟反向遍历数组,计算xi+1x_{i+1} ~ xn−1x_{n-1}的乘积Note原创 2015-12-11 22:04:16 · 457 阅读 · 0 评论 -
leetcode -- House Robber II -- 重点dp
https://leetcode.com/problems/house-robber-ii/注意思路:对于第一个house, 判断其“rob” or “not rob”. if rob, 那么结果就是nums[0] + rob_linear(nums[2:i-1]); if not rob, 结果就是 rob_linear(nums[1:i])参考: http://bookshadow.com/w原创 2015-12-10 17:32:13 · 430 阅读 · 0 评论 -
leetcode -- Game of Life -- 有trick,要看
https://leetcode.com/problems/game-of-life/参考http://bookshadow.com/weblog/2015/10/04/leetcode-game-life/原创 2015-12-10 20:21:49 · 646 阅读 · 0 评论 -
leetcode -- Permutation & Permutation II--重点
https://leetcode.com/problems/permutations/https://leetcode.com/problems/permutations-ii/思路1:递归如果不考虑重复 参考http://www.cnblogs.com/zuoyuan/p/3758816.html http://effyhuang.com/2014/08/06/permutations-lee原创 2015-12-13 13:54:40 · 1589 阅读 · 0 评论 -
回溯算法---重点
backtracking 实际上就是通过DFS策略实现的。看http://blog.youkuaiyun.com/daniel_ustc/article/details/17040315 有模板原创 2015-12-13 20:00:37 · 437 阅读 · 0 评论 -
leetcode -- Minimum Height Trees -- 关于graph的,重点
https://leetcode.com/problems/minimum-height-trees/有trick 对graph不熟悉。再看几遍。参考http://bookshadow.com/weblog/2015/11/26/leetcode-minimum-height-trees/原创 2015-12-10 22:29:42 · 2208 阅读 · 0 评论 -
leetcode -- Best Time to Buy and Sell Stock IV --难点要看
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/参考: http://bookshadow.com/weblog/2015/02/18/leetcode-best-time-to-buy-and-sell-stock-iv/ http://www.cnblogs.com/maples7/p/4350047.html原创 2015-12-28 18:34:36 · 545 阅读 · 0 评论 -
关于Tree相关问题总结---重点
preorder, inorder, postorder的recursive方法还有iterative的方法dfs以及bfsdfs就是pre order枚举所有从root到leaf的path给定一个node,找一条从root到它的path原创 2015-12-08 10:05:35 · 529 阅读 · 0 评论 -
leetcode -- Maximum Subarray -- 经典问题常考
https://leetcode.com/problems/maximum-subarray/参考http://fisherlei.blogspot.hk/2012/12/leetcode-maximum-subarray.html思路1:假设A(0, i)区间存在k,使得[k, i]区间是以i结尾区间的最大值, 定义为Max[i], 在这里,当求取Max[i+1]时,Max[i+1] = Ma原创 2015-12-11 23:49:38 · 1010 阅读 · 0 评论 -
leetcode -- Maximum Product Subarray -- 重点
https://leetcode.com/problems/maximum-product-subarray/参考思路:http://yucoding.blogspot.hk/2014/10/leetcode-quesion-maximum-product.html其实这里与Maximum subarray思路很像,都是dp经典。对max[i]的定义这里与Maximum subarray一致,只是要原创 2015-12-12 20:53:19 · 465 阅读 · 0 评论 -
leetcode -- Combination Sum -- 重点
https://leetcode.com/problems/combination-sum/这里参照dfs tree2 组合树的建树方法,因为有重合数字,所以子节点不再是只是A[k]后面的元素,子节点应该还是原来所有的元素。例如:参考http://www.cnblogs.com/TenosDoIt/p/3802647.html 比如对于数组3,2,6,7,target = 7,对数组排序得到[2,原创 2015-12-14 12:52:02 · 439 阅读 · 0 评论 -
leetcode -- 各种类型题目分类
http://algorithm.yuanbin.me/zh-cn/GLOSSARY.html#dfs转载 2015-12-14 16:57:08 · 1068 阅读 · 0 评论 -
leetcode -- Rotate Image -- 要看有trick
https://leetcode.com/problems/rotate-image/转置,再每行reverse即可 参考http://www.cnblogs.com/zuoyuan/p/3772978.htmlclass Solution(object): def rotate(self, matrix): """ :type matrix: List[L原创 2015-12-14 21:19:13 · 325 阅读 · 0 评论 -
leetcode -- Generate Parentheses -- DFS题目重点
https://leetcode.com/problems/generate-parentheses/这里用剩余左括号数目以及剩余右括号数目nl 以及nr来当做输入参数,逐个试探。subres 用 ”字符串来计算。class Solution(object): def dfs(self, nl, nr, subres, res): if nl == 0 and nr == 0:原创 2015-12-14 16:56:10 · 438 阅读 · 0 评论 -
leetcode -- Search for a Range -- 思路简单,要再做一遍
https://leetcode.com/problems/search-for-a-range/参考 http://www.cnblogs.com/zuoyuan/p/3775904.html思路 就是BS mid找到target之后,再左右搜索边界即可原创 2015-12-14 20:54:19 · 455 阅读 · 0 评论 -
leetcode -- Search Insert Position -- BS简单重点
https://leetcode.com/problems/search-insert-position/思路就是 BS,只要最后记得return low就行参考http://www.cnblogs.com/zuoyuan/p/3772705.html原创 2015-12-14 20:59:41 · 354 阅读 · 0 评论