![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
like
feeeeeeet
这个作者很懒,什么都没留下…
展开
-
Leetcode 11. Container With Most Water
Two Pointer Algorithm & Proofclass Solution { public int maxArea(int[] height) { int left=0; int right=height.length-1; int sum=0; while(left<right){ ...原创 2018-11-13 06:02:36 · 141 阅读 · 0 评论 -
Leetcode 77. Combinations
recursion &dfsrecursion(slow)class Solution: def combine(self, n, k): """ :type n: int :type k: int :rtype: List[List[int]] """ res = [] ...原创 2018-12-29 06:52:33 · 129 阅读 · 0 评论 -
Leetcode 78 & 90. Subsets I&II
class Solution: def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res=[] self.dfs(nums,0,[],res) return r原创 2018-12-30 00:44:53 · 101 阅读 · 0 评论 -
Leetcode 79. Word Search
dfs66.9%class Solution: def exist(self, board, word): """ :type board: List[List[str]] :type word: str :rtype: bool """ def dfs(index,row...原创 2018-12-30 09:18:19 · 95 阅读 · 0 评论 -
Leetcode 80. Remove Duplicates from Sorted Array II
2 methodsclass Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 newT...原创 2018-12-30 09:35:30 · 74 阅读 · 0 评论 -
Leetcode 82&83. Remove Duplicates from Sorted List I & II
recursion can be used in IIdifference between pre=cur(the same node) and pre.val=cur.val (two nodes have the same number)Remove Duplicates from Sorted List I2 solutionsslow version# Definition fo...原创 2018-12-31 00:24:20 · 111 阅读 · 0 评论 -
Leetcode 84. Largest Rectangle in Histogram
stackexplaination(small mistakes in the graphs): http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.htmlclass Solution(object): def largestRectangleArea(self, height): ...原创 2018-12-31 04:18:52 · 94 阅读 · 0 评论 -
Leetcode 94 & 102 & 144 & 145. Binary Tree Traversal (dfs & bfs)
stack# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def inorder...原创 2018-12-31 20:00:57 · 112 阅读 · 0 评论 -
Leetcode 95 & 96. Unique Binary Search Trees I & II
Unique Binary Search Trees IGiven n, how many structurally unique BST’s (binary search trees) that store values 1 … n?Example:Input: 3Output: 5class Solution(object): def numTrees(self, n)...原创 2019-01-02 05:04:54 · 77 阅读 · 0 评论 -
Leetcode 104 & 111. Minimum & Maximum Depth of Binary Tree
Maximum Depth of Binary Tree# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass So...原创 2019-01-07 07:27:14 · 86 阅读 · 0 评论 -
Leetcode 102 & 107. Binary Tree Level Order Traversal I & II
Binary Tree Level Order TraversalGiven a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example:Given...原创 2019-01-18 03:41:03 · 96 阅读 · 0 评论 -
Leetcode 105 & 106. Construct Binary Tree from Preorder and Inorder & Inorder and Postorder
hashUse a dictionary to store the number in inorder and its index. Find position of the root in inorder in O(1).slow version# Definition for a binary tree node.# class TreeNode:# def __init__...原创 2019-01-18 05:36:16 · 107 阅读 · 0 评论 -
Leetcode 75. Sort Colors
Two pointersleft points to the position that 0 endsright points to the position that 2 beginsclass Solution: def sortColors(self, nums): """ :type nums: List[int] :rtype...原创 2018-12-29 04:48:23 · 70 阅读 · 0 评论 -
Leetcode 70. Climbing Stairs
recursion & dp(Fibonacci Number)Recursion!TLEclass Solution: def climbStairs(self, n): """ :type n: int :rtype: int """ if n == 1: retur...原创 2018-12-28 23:43:58 · 79 阅读 · 0 评论 -
Leetcode 60. Permutation Sequence
n=4,k=9There are 4 groups of n=4 (1-6,7-12,13-18,19-24), 9 belongs to the second one. So the first number is the second one of [1,2,3,4], which is 2.There are 3 groups of n=3 (1-2,3-4,5-6), the rema...原创 2018-12-12 08:36:11 · 94 阅读 · 0 评论 -
Leetcode 15. 3Sum
Iterate every unduplicated element in nums.In every iteration, use two pointers to find another two numbers from both sides to center.First version gives a wrong answer for the following case:In...原创 2018-11-14 05:23:29 · 62 阅读 · 0 评论 -
Leetcode 19. Remove Nth Node From End of List
Faster pointer go first, after n steps there are len-n numbers. Then two pointers go together until there is no number faster pointer points to(len-n steps). Slower pointer points to the n-th node fro...原创 2018-11-14 09:09:36 · 59 阅读 · 0 评论 -
Leetcode 23. Merge k Sorted Lists
3 solutions- Compare one by one &amp; Heap &amp;MergeSortSolution 1- Compare one by one (slow)class Solution { public ListNode mergeKLists(ListNode[] lists) { ListNode head=new ListNode(...原创 2018-11-16 06:20:21 · 136 阅读 · 0 评论 -
Leetcode 25. Reverse Nodes in k-Group
extend from Leetcode 240(q)-1(head)-2-3-4-5-60(q)-2-1(head)-3-4-5-60-3-2-1(q)-4(head)-5-6class Solution { public ListNode reverseKGroup(ListNode head, int k) { ListNode res=new ListNode(0);...原创 2018-11-19 06:19:57 · 57 阅读 · 0 评论 -
Leetcode 21. Merge Two Sorted Lists
Complicated VersionPart 1 should be the first part, otherwise the program couldn’t pass the following case. In the last iteration, we have flag=0 and l2==null, so the program can’t run part 2.input...原创 2018-11-15 03:44:32 · 63 阅读 · 0 评论 -
Leetcode 24. Swap Nodes in Pairs
2 solutions - use two extra pointers & recursiveSolution 1 use two extra pointersclass Solution { public ListNode swapPairs(ListNode head) { ListNode res=new ListNode(-1); re...原创 2018-11-15 06:17:42 · 163 阅读 · 0 评论 -
Leetcode 39 & 40. Combination Sum I &II
Combination Sum Iclass Solution: def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] ""...原创 2018-12-04 12:00:01 · 90 阅读 · 0 评论 -
Leetcode 50. Pow(x, n)
class Solution: def myPow(self, x, n): i=abs(n) res = 1.0 while i: if i%2!=0: res = res*x print(res,x) x=x*x ...原创 2018-12-05 23:40:10 · 78 阅读 · 0 评论 -
Leetcode 45&55. Jump Game I &II
There are many solutions for these two problems. For Jump Game I, greedy is the best method, but we can also use DP to solve it.Jump Game ISolution 1 DPThere are two kind of DP solution, bottom-up...原创 2018-12-13 01:02:27 · 111 阅读 · 0 评论 -
Leetcode 69. Sqrt(x)
Binary Search and Newton MethodBinary Searchclass Solution: def mySqrt(self, x): """ :type x: int :rtype: int """ l, r = 0, x while l &原创 2018-12-27 06:29:17 · 72 阅读 · 0 评论 -
Leetcode 53. Maximum Subarray
max(sum,tmp+currentnum,currentnum)If currentnum+tmp>tmp and tmp<0(such as tmp=-2,currentnum=4), discard the elements before current element.class Solution(object): def maxSubArray(self, nu...原创 2018-12-12 05:32:00 · 68 阅读 · 0 评论 -
Leetcode 5. Longest Palindromic Substring
3 solutions-Dynamic Programming &amp; Expand Around Center &amp;Solution 1 Dynamic ProgrammingThe process of compute dp[i][j] is showed as following:0000 011100 01 0211 122200 01 02 0311 12...原创 2018-11-12 04:01:51 · 84 阅读 · 0 评论