like
feeeeeeet
这个作者很懒,什么都没留下…
展开
-
Leetcode 11. Container With Most Water
Two Pointer Algorithm & Proof class 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 · 144 阅读 · 0 评论 -
Leetcode 77. Combinations
recursion &dfs recursion(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 · 132 阅读 · 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 · 105 阅读 · 0 评论 -
Leetcode 79. Word Search
dfs 66.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 · 98 阅读 · 0 评论 -
Leetcode 80. Remove Duplicates from Sorted Array II
2 methods class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 newT...原创 2018-12-30 09:35:30 · 77 阅读 · 0 评论 -
Leetcode 82&83. Remove Duplicates from Sorted List I & II
recursion can be used in II difference between pre=cur(the same node) and pre.val=cur.val (two nodes have the same number) Remove Duplicates from Sorted List I 2 solutions slow version # Definition fo...原创 2018-12-31 00:24:20 · 112 阅读 · 0 评论 -
Leetcode 84. Largest Rectangle in Histogram
stack explaination(small mistakes in the graphs): http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html class Solution(object): def largestRectangleArea(self, height): ...原创 2018-12-31 04:18:52 · 96 阅读 · 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 = None class Solution: def inorder...原创 2018-12-31 20:00:57 · 116 阅读 · 0 评论 -
Leetcode 95 & 96. Unique Binary Search Trees I & II
Unique Binary Search Trees I Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n? Example: Input: 3 Output: 5 class Solution(object): def numTrees(self, n)...原创 2019-01-02 05:04:54 · 83 阅读 · 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 = None class So...原创 2019-01-07 07:27:14 · 87 阅读 · 0 评论 -
Leetcode 102 & 107. Binary Tree Level Order Traversal I & II
Binary Tree Level Order Traversal Given 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 · 101 阅读 · 0 评论 -
Leetcode 105 & 106. Construct Binary Tree from Preorder and Inorder & Inorder and Postorder
hash Use 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 · 111 阅读 · 0 评论 -
Leetcode 75. Sort Colors
Two pointers left points to the position that 0 ends right points to the position that 2 begins class Solution: def sortColors(self, nums): """ :type nums: List[int] :rtype...原创 2018-12-29 04:48:23 · 74 阅读 · 0 评论 -
Leetcode 70. Climbing Stairs
recursion & dp(Fibonacci Number) Recursion !TLE class Solution: def climbStairs(self, n): """ :type n: int :rtype: int """ if n == 1: retur...原创 2018-12-28 23:43:58 · 82 阅读 · 0 评论 -
Leetcode 60. Permutation Sequence
n=4,k=9 There 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 · 99 阅读 · 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 · 64 阅读 · 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 · 63 阅读 · 0 评论 -
Leetcode 23. Merge k Sorted Lists
3 solutions- Compare one by one &amp; Heap &amp;MergeSort Solution 1- Compare one by one (slow) class Solution { public ListNode mergeKLists(ListNode[] lists) { ListNode head=new ListNode(...原创 2018-11-16 06:20:21 · 138 阅读 · 0 评论 -
Leetcode 25. Reverse Nodes in k-Group
extend from Leetcode 24 0(q)-1(head)-2-3-4-5-6 0(q)-2-1(head)-3-4-5-6 0-3-2-1(q)-4(head)-5-6 class Solution { public ListNode reverseKGroup(ListNode head, int k) { ListNode res=new ListNode(0); ...原创 2018-11-19 06:19:57 · 60 阅读 · 0 评论 -
Leetcode 21. Merge Two Sorted Lists
Complicated Version Part 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 · 67 阅读 · 0 评论 -
Leetcode 24. Swap Nodes in Pairs
2 solutions - use two extra pointers & recursive Solution 1 use two extra pointers class Solution { public ListNode swapPairs(ListNode head) { ListNode res=new ListNode(-1); re...原创 2018-11-15 06:17:42 · 180 阅读 · 0 评论 -
Leetcode 39 & 40. Combination Sum I &II
Combination Sum I class Solution: def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] ""...原创 2018-12-04 12:00:01 · 92 阅读 · 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 · 81 阅读 · 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 I Solution 1 DP There are two kind of DP solution, bottom-up...原创 2018-12-13 01:02:27 · 126 阅读 · 0 评论 -
Leetcode 69. Sqrt(x)
Binary Search and Newton Method Binary Search class Solution: def mySqrt(self, x): """ :type x: int :rtype: int """ l, r = 0, x while l &原创 2018-12-27 06:29:17 · 75 阅读 · 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 · 70 阅读 · 0 评论 -
Leetcode 5. Longest Palindromic Substring
3 solutions-Dynamic Programming &amp; Expand Around Center &amp; Solution 1 Dynamic Programming The process of compute dp[i][j] is showed as following: 00 00 01 11 00 01 02 11 12 22 00 01 02 03 11 12...原创 2018-11-12 04:01:51 · 89 阅读 · 0 评论