python
feeeeeeet
这个作者很懒,什么都没留下…
展开
-
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 · 144 阅读 · 0 评论 -
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 · 88 阅读 · 0 评论 -
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 · 92 阅读 · 0 评论 -
Leetcode 85. Maximal Rectangle
same method to leetcode 84reference:https://blog.csdn.net/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 · 138 阅读 · 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 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 评论 -
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 · 83 阅读 · 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 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 · 96 阅读 · 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 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 · 131 阅读 · 0 评论 -
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 · 98 阅读 · 0 评论 -
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 · 100 阅读 · 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 · 114 阅读 · 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 · 109 阅读 · 0 评论 -
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 · 98 阅读 · 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 · 97 阅读 · 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 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 · 92 阅读 · 0 评论 -
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 · 118 阅读 · 0 评论 -
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 · 74 阅读 · 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 · 80 阅读 · 0 评论 -
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 · 67 阅读 · 0 评论 -
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 · 82 阅读 · 0 评论 -
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 · 82 阅读 · 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 · 72 阅读 · 0 评论 -
Leetcode 74. Search a 2D Matrix
Binary Searchposition01234567891011number1458911121517182022row000011112222col012301230123row=position//4col=position%4原创 2018-12-29 03:11:11 · 84 阅读 · 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 · 119 阅读 · 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 · 79 阅读 · 0 评论 -
Leetcode 49. Group Anagrams
class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ strings, res, ans = [], {}, [] for s in...原创 2018-12-05 23:38:09 · 58 阅读 · 0 评论 -
Leetcode 48. Rotate Image
Fold along the middle line and then fold along the diagonal line.1 2 3 \space \space \space \space 3 2 1 \space \space ...原创 2018-12-05 23:21:09 · 86 阅读 · 0 评论 -
Leetcode 46 & 47. Permutations I & II
BacktrackingPermutations Iclass Solution: def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res=[] tmp=[] sel...原创 2018-12-05 23:09:58 · 80 阅读 · 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 · 91 阅读 · 0 评论 -
Leetcode 38. Count and Say
class Solution: def countAndSay(self, n): """ :type n: int :rtype: str """ res="1" if n==1: return res for i in range(n-1): ...原创 2018-12-04 11:53:44 · 65 阅读 · 0 评论 -
Leetcode 36. Valid Sudoku
How to decide which block the element belongs to: 3*(i//3)+j//3class Solution: def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ ...原创 2018-12-04 11:51:06 · 138 阅读 · 2 评论 -
Leetcode 35. Search Insert Position
class Solution: def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ start=0 end=len(nums)-1 ...原创 2018-12-04 11:35:47 · 65 阅读 · 0 评论 -
Leetcode 34. Find First and Last Position of Element in Sorted Array
Binary SearchSimilar to Leetcode 33class Solution: def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """...原创 2018-12-04 11:34:17 · 58 阅读 · 0 评论 -
Leetcode 33. Search in Rotated Sorted Array
Divide the array into two parts and at least one of them is sorted. Then decide which part the target belongs to, change the ‘mid’ pointer to new start or end.Slow versionclass Solution: def sea...原创 2018-12-04 11:31:37 · 80 阅读 · 0 评论 -
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 · 72 阅读 · 0 评论 -
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 · 80 阅读 · 0 评论