- 博客(80)
- 收藏
- 关注
原创 Leetcode 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the ...
2019-01-18 06:39:35
177
原创 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
140
原创 Leetcode 103. Binary Tree Zigzag Level Order Traversal
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def zigzagLevelOr...
2019-01-18 04:51:46
131
原创 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
122
原创 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
118
原创 Leetcode 100 & 101. Same Tree & Symmetric Tree
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def isSameTree(se...
2019-01-07 03:33:12
122
原创 Leetcode 99. Recover Binary Search Tree
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def recoverTree(s...
2019-01-07 03:31:36
144
原创 Leetcode 98. Validate Binary Search Tree
Same method to inorder traversal# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = No...
2019-01-02 05:39:04
101
原创 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
106
原创 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
149
原创 Leetcode 89. Gray Code
2 solutionsclass Solution: def grayCode(self, n): """ :type n: int :rtype: List[int] """ return map(lambda x:int(x,2),self.gray(n)) def gray(self,n):...
2018-12-31 19:36:00
124
原创 Leetcode 88. Merge Sorted Array
class Solution: def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype: void Do no...
2018-12-31 07:49:46
120
原创 Leetcode 86. Partition List
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def partition(self, head, x): """ ...
2018-12-31 07:08:29
118
原创 Leetcode 85. Maximal Rectangle
same method to leetcode 84reference:https://blog.youkuaiyun.com/doc_sgl/article/details/11832965https://www.cnblogs.com/lupx/archive/2015/10/20/leetcode-85.htmlclass Solution(object): def maximalRec...
2018-12-31 06:52:27
165
原创 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
127
原创 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
132
原创 Leetcode81. Search in Rotated Sorted Array II
class Solution: def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: bool """ left=0 right=len(nums)-1
2018-12-30 23:30:25
111
原创 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
106
原创 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
118
原创 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
126
原创 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
162
原创 Leetcode 76. Minimum Window Substring
sliding window: find a window that contains all the characters in t, then reduce the window by moving start pointer but still contains all the characters.class Solution: def minWindow(self, s, t)...
2018-12-29 06:10:35
124
原创 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
96
原创 Leetcode 74. Search a 2D Matrix
Binary Searchposition01234567891011number1458911121517182022row000011112222col012301230123row=position//4col=position%4
2018-12-29 03:11:11
110
原创 Leetcode 73. Set Matrix Zeroes
class Solution: def setZeroes(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place instead. """
2018-12-29 00:38:17
93
原创 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
101
原创 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
92
原创 Leetcode 68. Text Justification
class Solution: def fullJustify(self, words, maxWidth): """ :type words: List[str] :type maxWidth: int :rtype: List[str] """ res, cur, num_of_l
2018-12-27 06:25:04
195
原创 Leetcode 67. Add Binary
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ m=len(a)-1 n=len(b)-1 res=''
2018-12-27 06:07:00
87
原创 Leetcode 66. Plus One
99.83%class Solution: def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ res=[] tmp=0 for i in digits[::-1
2018-12-27 01:02:19
91
原创 Leetcode 65. Valid Number
s=s.strip() delete the space front and end.‘e’ can only appear after digits and ‘.’ cannot appear after ‘e’. In other words, the character before ‘e’ and after ‘e’ must be both digits. (e3 and 3e ar...
2018-12-27 00:38:19
138
原创 Leetcode 64. Minimum Path Sum
class Solution: def minPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ m=len(grid) n=len(grid[0]) for i in range(m): ...
2018-12-26 01:36:48
89
原创 Leetcode 62 & 63. Unique Paths I &II
key point: Unique Paths II fast versionUnique Paths Iclass Solution: def uniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ dp...
2018-12-26 01:22:34
108
原创 Leetcode 61. Rotate List
Calculate the length of the list and use k%length.# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution...
2018-12-26 00:29:28
106
原创 Leetcode 57. Insert Interval
# Definition for an interval.# class Interval:# def __init__(self, s=0, e=0):# self.start = s# self.end = eclass Solution: def insert(self, intervals, newInterval): ...
2018-12-13 04:59:57
105
原创 Leetcode 56. Merge Intervals
# Definition for an interval.# class Interval:# def __init__(self, s=0, e=0):# self.start = s# self.end = eclass Solution: def merge(self, intervals): """ ...
2018-12-13 03:22:22
97
原创 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
182
原创 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
118
原创 Leetcode 58. Length of Last Word
special case “a b ”class Solution(object): def lengthOfLastWord(self, s): """ :type s: str :rtype: int """ result=0 index=len(s)-1 whi...
2018-12-12 07:58:26
99
原创 Leetcode 54 & 59. Spiral Matrix I & II
Spiral Matrix Iclass Solution: def spiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ if len(matrix) == 0: retu...
2018-12-12 07:08:58
133
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人