
leetcode
coderate
这个作者很懒,什么都没留下…
展开
-
LeetCode---154. Find Minimum in Rotated Sorted Array II
题目给出旋转数组,找出旋转数组中最小的值。旋转数组中可能有重复元素。如:Example 1:Input: [1,3,5] Output: 1Example 2:Input: [2,2,2,0,1] Output: 0Python题解class Solution: def findMin(self, nums): """ :...原创 2018-05-31 15:08:55 · 204 阅读 · 0 评论 -
LeetCode---670. Maximum Swap
题目给出一个非负整数,最多交换一次两个数字,找出能发现的最大整数。如果不存在,返回原数。如:Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7.Example 2: Input: 9973 Output: 9973 Explanation: No swap....原创 2018-05-29 12:55:44 · 256 阅读 · 0 评论 -
LeetCode---718. Maximum Length of Repeated Subarray
题目给出两个整数数组A和B,找出A和B的最长子数组,输出最长子数组的长度。Python题解class Solution(object): def findLength(self, A, B): """ :type A: List[int] :type B: List[int] :rtype: int ...原创 2018-05-29 11:57:45 · 227 阅读 · 0 评论 -
LeetCode---90. Subsets II
题目给出一个数组,里面可能包含重复元素,返回所有的子数组。注意:不能包含重复的子数组。Python题解class Solution(object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] ...原创 2018-05-29 11:11:23 · 229 阅读 · 0 评论 -
LeetCode---64. Minimum Path Sum
题目给出一个m * n的网格,网格里面的数字是非负数。找出一条从[0, 0]到[m -1, n - 1]的路径,使得路径上元素的和最小。注意:你每次只能向右和向下走一步。如:Example:Input: [ [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because the path 1→3→1→1→1...原创 2018-05-29 10:47:20 · 195 阅读 · 0 评论 -
LeetCode---56. Merge Intervals
题目给出一个间隔的数组,合并所有重叠的间隔。如: Example 1:Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]]Example 2:Input: [[1,4],[4,5]] Output: [[1,5]]Python题解class Solution(object): ...原创 2018-05-29 10:24:31 · 183 阅读 · 0 评论 -
LeetCode---40. Combination Sum II
题目给出一个数组和目标值,找出所有的组合,使得组合里面元素的和为目标值。注意:原数组可能有重复元素;数组中的元素在组合里只能出现一次。如:Example 1:Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1,...原创 2018-05-29 10:07:31 · 184 阅读 · 0 评论 -
LeetCode---39. Combination Sum
题目给出一个数组,没有重复元素,另给出一个目标值,找出所有的组合,组合的和为目标值。注意:相同的元素可以重复无限次。Python题解class Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] ...原创 2018-05-28 16:31:08 · 182 阅读 · 0 评论 -
LeetCode---33. Search in Rotated Sorted Array
题目给出一个递增有序数组,以某个元素为准旋转,如[0,1,2,4,5,6,7]变成了[4,5,6,7,0,1,2]。另给出一个目标值,在这个数组中寻找,如果找到返回index,否则返回-1。数组中没有重复元素,你的算法时间复杂度必须为O(logn)。Python题解class Solution(object): def search(self, nums, target):...原创 2018-05-28 16:11:48 · 178 阅读 · 0 评论 -
LeetCode---229. Majority Element II
题目给出一个整数数组,找出所有超过n / 3次的数。算法时间复杂度必须是O(n),空间复杂度为O(1)。Python题解class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: List[int] ...原创 2018-05-28 15:52:35 · 196 阅读 · 0 评论 -
LeetCode---713. Subarray Product Less Than K
题目给出一个正整数数组,找出子序列,满足子序列的乘积小于指定值。输出符合要求的子序列的个数。如:Example 1: Input: nums = [10, 5, 2, 6], k = 100 Output: 8 Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], ...原创 2018-05-29 13:15:15 · 369 阅读 · 0 评论 -
LeetCode---287. Find the Duplicate Number
题目给出一个含有n+1个元素的数组,里面的元素值在1到n的范围内,找出重复的数字,假设只存在一个重复的数字,该数字的次数可能出现多次。Python题解class Solution: def findDuplicate(self, nums): """ :type nums: List[int] :rtype: bool ...原创 2018-05-29 14:18:03 · 178 阅读 · 0 评论 -
LeetCode---81. Search in Rotated Sorted Array II
题目给出一个旋转数组和目标值,在旋转数组中找到目标值。旋转数组中可能包含重复元素。如:Example 1:Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true Example 2:Input: nums = [2,5,6,0,0,1,2], target = 3 Output: falsePython题解cl...原创 2018-05-31 14:11:43 · 195 阅读 · 0 评论 -
LeetCode---79. Word Search
题目给出一个m * n的二维数组,里面的元素是字母,另给出一个单词,找出是否可以利用二维数组相邻的字母来构造单词。相邻意味着4个方向:上下左右。如:Example:board = [ [‘A’,’B’,’C’,’E’], [‘S’,’F’,’C’,’S’], [‘A’,’D’,’E’,’E’] ]Given word = “ABCCED”, return tr...原创 2018-05-31 11:24:59 · 219 阅读 · 0 评论 -
LeetCode---153. Find Minimum in Rotated Sorted Array
题目给出一个旋转数组,找出旋转数组中最小的值。如:Example 1:Input: [3,4,5,1,2] Output: 1 Example 2:Input: [4,5,6,7,0,1,2] Output: 0Python题解class Solution(object): def findMin(self, nums): """ ...原创 2018-05-31 10:38:28 · 201 阅读 · 0 评论 -
LeetCode---209. Minimum Size Subarray Sum
题目给出一个数组和目标值s,找出一个连续子序列,使得和不小于目标值。找出长度最小的连续子序列。如:Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarray [4,3] has the minimal length under the problem constraint.Pyt...原创 2018-05-31 10:29:57 · 299 阅读 · 0 评论 -
LeetCode---216. Combination Sum III
题目给出n和k,从1到9中选出k个数,使得k个数的和为n。1到9每个数只能使用1次。Python题解class Solution(object): def combinationSum3(self, k, n): """ :type k: int :type n: int :rtype: List[List[i...原创 2018-05-31 10:04:39 · 246 阅读 · 0 评论 -
LeetCode---179. Largest Number
题目给出一个非负整数的数组,组合它们,找出能组成的最大的数。因为结果可能非常大,你需要哦返回一个字符串。如:Example 1:Input: [10,2] Output: “210” Example 2:Input: [3,30,34,5,9] Output: “9534330”Note: The result may be very large, so you need...原创 2018-05-30 20:05:25 · 253 阅读 · 0 评论 -
LeetCode---560. Subarray Sum Equals K
题目给出一个整数数组和目标值,你需要找到所有的连续子序列,该子序列的和为目标值。输出满足该条件的子序列的个数。Python题解class Solution: def subarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype...原创 2018-05-29 15:04:46 · 223 阅读 · 0 评论 -
LeetCode---152. Maximum Product Subarray
题目给出一个整数数组,找到一个连续子序列,该子序列具有最大的乘积。Python题解class Solution(object): def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ maxnum = big ...原创 2018-05-29 14:44:56 · 184 阅读 · 0 评论 -
LeetCode---556. Next Greater Element III
题目给出一个32bit的正整数n,你需要找到一个这样的数,这个数和原来的数含有相同的数字,但是新数是比原数大的最小的数。如果不存在就放回-1。如:Example 1:Input: 12 Output: 21Example 2:Input: 21 Output: -1Python题解class Solution: def nextGreaterElemen...原创 2018-05-28 14:57:24 · 340 阅读 · 0 评论 -
LeetCode---31. Next Permutation
题目给出一个序列,按照字典序重新安排序列元素的顺序,使得新的序列是下一个比原来序列大。如果不存在,就将序列逆序。Python题解class Solution(object): def nextPermutation(self, nums): """ :type nums: List[int] :rtype: void Do...原创 2018-05-28 14:42:41 · 225 阅读 · 0 评论 -
LeetCode----18. 4Sum
题目给出一个数组和一个目标值,找出4个元素,使得它们的和为目标值。找出所有满足该条件的四元组。Python题解class Solution: def fourSum(self,nums, target): """ :type nums: List[int] :type target: int :rtype:...原创 2018-05-28 11:55:20 · 184 阅读 · 0 评论 -
LeetCode---687. Longest Univalue Path
题目给出一个二叉树,找出一条最长的路径,该路径上的节点值都相同。这个路径可能经过根节点。Python题解class Solution(object): def longestUnivaluePath(self, root): """ :type root: TreeNode :rtype: int """ ...原创 2018-05-06 19:42:27 · 142 阅读 · 0 评论 -
LeetCode---543. Diameter of Binary Tree
题目给出一个二叉树,计算出树的直径。二叉树的直径指的是两个节点的路径的最大长度。这个路径可能会经过根节点。Python题解class Solution(object): def diameterOfBinaryTree(self, root): """ :type root: TreeNode :rtype: int ...原创 2018-05-06 19:38:19 · 154 阅读 · 0 评论 -
LeetCode---108. Convert Sorted Array to Binary Search Tree
题目给出一个排好序的数组,将其转换成高度平衡的二叉搜索树。高度平衡意味着每个节点的左右子树的高度差不大于1。Python题解class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode ...原创 2018-05-06 19:35:44 · 157 阅读 · 0 评论 -
LeetCode---112. Path Sum
题目给出一个二叉树和一个值,判断这个二叉树是否有一条从根节点到叶节点的路径,这条路径上节点值的和等于这个给定的值。Python题解class Solution(object): def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int ...原创 2018-05-06 16:54:15 · 151 阅读 · 0 评论 -
LeetCode---111. Minimum Depth of Binary Tree
题目:给出一个二叉树,发现它的最小深度。最小深度指的是从根节点到最近的叶节点的路径的长度。Python题解class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if...原创 2018-05-06 16:49:18 · 160 阅读 · 0 评论 -
LeetCode---606. Construct String from Binary Tree
题目你需要通过前序遍历一个二叉树,构造出一个由数字和括号组成的字符串。null节点有空括号来表示“()”。你需要忽略所有的空括号对如果它不影响字符串和原始二叉树的对应关系。如下例子所示: 例子一: Input: Binary tree: [1,2,3,4] 1 / \ 2 3 / 4 Output: “1(2(4)...原创 2018-05-06 16:35:25 · 146 阅读 · 0 评论 -
LeetCode---653. Two Sum IV - Input is a BST
题目给出一个二叉搜索树和一个目标值,找到两个节点值得和为给定的目标值。Python题解class Solution(object): def findTarget(self, root, k): """ :type root: TreeNode :type k: int :rtype: bool ...原创 2018-05-06 16:25:55 · 189 阅读 · 0 评论 -
LeetCode---226. Invert Binary Tree
题目给出一个二叉树,将其左右节点翻转。Python题解一:递归class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root is None: ...原创 2018-05-06 16:20:44 · 258 阅读 · 0 评论 -
LeetCode---104. Maximum Depth of Binary Tree
题目给出一个二叉树,找到它的最大深度。最大深度指的是根节点到最远叶节点的路径的长度。Python题解class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ if root...原创 2018-05-06 16:16:08 · 165 阅读 · 0 评论 -
LeetCode---235. Lowest Common Ancestor of a Binary Search Tree
题目给出一个二叉搜索树,找出给定两个节点的最低公共祖先。Python题解class Solution(object): def lowestCommonAncestor(self, root, p, q): """ :type root: TreeNode :type p: TreeNode :type q...原创 2018-05-07 09:43:36 · 144 阅读 · 0 评论 -
LeetCode---二叉树题目整理Easy篇
101. Symmetric Tree【Easy】 https://blog.youkuaiyun.com/leel0330/article/details/80228022104. Maximum Depth of Binary Tree【Easy】 https://blog.youkuaiyun.com/leel0330/article/details/80215507108. C...原创 2018-05-07 16:47:56 · 326 阅读 · 0 评论 -
LeetCode---16. 3Sum Closest
题目给出一个数组和一个目标值,找出三个数,使得其和和目标值最接近。假设数组中只有一个结果满足该条件。Python题解class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: i...原创 2018-05-28 11:44:04 · 234 阅读 · 0 评论 -
LeetCode---15. 3Sum
题目给定一个数组,找出3个元素a,b,c使得它们的和为0。找出所有符合该条件的三元组。Python题解class Solution: def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ n...原创 2018-05-28 11:11:44 · 252 阅读 · 0 评论 -
LeetCode---数组题目整理Easy篇
532. K-diff Pairs in an Array665. Non-decreasing Array原创 2018-05-28 11:00:56 · 617 阅读 · 0 评论 -
LeetCode---数组题目整理Medium篇
15. 3Sum556. Next Greater Element III原创 2018-05-28 10:59:38 · 876 阅读 · 0 评论 -
LeetCode---437. Path Sum III
题目给出一个二叉树和一个目标值值,找到所有的路径,该路径上的节点数值和为目标值。路径不一定非要用根节点开始。举个例子:root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \ 3 -2 1Retur...原创 2018-05-07 16:57:22 · 176 阅读 · 0 评论 -
LeetCode---110. Balanced Binary Tree
题目给出一个二叉树,判断其是否是平衡二叉树。Python题解class Solution(object): def dfs(self, root): if not root: return 0 d1 = self.dfs(root.left) if d1 == -1: re...原创 2018-05-07 16:54:25 · 131 阅读 · 0 评论