![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
lulin916
这个作者很懒,什么都没留下…
展开
-
31/100. Palindromic Substrings
DP算法:①单个字符是回文;②两个连续字符,如果相等则是回文;③3个以上的字符,两头相等且去掉首尾后依然是回文。以 s=‘aba’为例:[0][0][0][1][0][2][1][0][1][1][1][2][2][0][2][1][2][2]class Solution(object): def countSubstrin...原创 2019-01-03 15:46:45 · 155 阅读 · 0 评论 -
25/100. Palindrome Linked List
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def isPalindrome(self, head)...原创 2018-12-18 21:22:37 · 96 阅读 · 0 评论 -
24/100. Linked List Cycle
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def hasCycle(self, head): ...原创 2018-12-18 11:49:04 · 81 阅读 · 0 评论 -
23/100. Valid Parentheses
class Solution: def isValid(self, s): """ :type s: str :rtype: bool """ if not s: return Tr原创 2018-12-18 10:01:21 · 105 阅读 · 0 评论 -
22/100. Find All Anagrams in a String
原创 2018-12-17 22:47:59 · 102 阅读 · 0 评论 -
29/100. Counting Bits
O(n)空间复杂度、O(n)时间复杂度:class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] "&quot原创 2018-12-19 16:26:28 · 102 阅读 · 0 评论 -
28/100. Shortest Unsorted Continuous Subarray
class Solution(object): def findUnsortedSubarray(self, nums): """ :type nums: List[int] :rtype: int """ i = 0 j = len(n原创 2018-12-19 15:27:00 · 124 阅读 · 0 评论 -
21/100. Two Sum
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] ""&原创 2018-12-06 11:51:16 · 101 阅读 · 0 评论 -
20/100. House Robber
class Solution: def rob(self, nums): """ :type nums: List[int] :rtype: int """ last = 0 now = 0 for i in nums:原创 2018-12-06 10:37:16 · 85 阅读 · 0 评论 -
26/100. Min Stack
class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.stack = [] def push(self,原创 2018-12-18 22:08:15 · 77 阅读 · 0 评论 -
27/100. Intersection of Two Linked Lists
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def getIntersectionNode(s...原创 2018-12-19 09:34:32 · 100 阅读 · 0 评论 -
38/100. Find the Duplicate Number
给定一个包含n+1个整数的数组nums,这些整数都在[1, n]范围内。假设肯定存在一个重复的整数,求这个整数。附加要求:①不能修改数组(假设nums是只读的);②空间复杂度要求为O(1);③时间复杂度要求小于O(n2);④数组中只有一个重复的数字,但是它可以重复多次。条件①限制不能使用sorted()对原数组进行排序,条件②限制不能使用dict进行记录,条件③限制不能使用for循环...原创 2019-01-10 11:29:55 · 110 阅读 · 0 评论 -
37/100. Subsets
给定一个整数数组nums,里面的数值皆不相同,要求返回所有可能的子集。使用DFS算法,具体思路如下图所示:class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] &a原创 2019-01-09 18:49:59 · 135 阅读 · 0 评论 -
36/100. Generate Parentheses
给定n对括号,要求输出所有合法的括号排列。使用DFS算法,主要分为两种情况:①若有剩余的“(”,则添加“(”后继续递归;②若有剩余的“)”,且“)”的个数多于“(”,即为排好的“(”个数多于“)”,这是判定是否合法的条件,则添加“)”后继续递归。class Solution(object): def __init__(self): self.res = [] ...原创 2019-01-08 21:43:25 · 120 阅读 · 1 评论 -
35/100. Permutations
给定一个整数数组,求它的全排列。思路一:使用Python的库函数itertoos.permutations。from itertools import permutationsfrom itertools import permutationsclass Solution(object): def permute(self, nums): """原创 2019-01-08 18:53:25 · 191 阅读 · 0 评论 -
34/100. Top K Frequent Elements
给定一个非空的整数数组,要求返回k个最常见的元素,即返回k个高频整数。题目要求时间复杂度要优于O(n logn),那也就是不能直接对数组排序然后返回前k个不等的整数。思路一:使用dict用来记录数字及其出现的次数,然后对dict进行排序,返回要求结果,勉强符合题目要求。class Solution(object): def topKFrequent(self, nums, k):...原创 2019-01-08 17:37:12 · 93 阅读 · 0 评论 -
33/100. Product of Array Except Self
给定一个长度大于1的数组nums,输出一个数组,其第i位的元素为除nums[i]的其它元素的乘积。要求不使用除法,且时间复杂度为O(n)。如下表所示,构造数组nums1和nums2,对应位相乘即为题目要求的结果。为保证空间复杂度为O(0),则可以用常量p来代替数组。index012numsa1a2a3resa2*a3a1*a3a1*a2...原创 2019-01-08 16:51:27 · 89 阅读 · 0 评论 -
32/100. Binary Tree Inorder Traversal
中序遍历二叉树。递归法:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution...原创 2019-01-08 11:50:40 · 130 阅读 · 0 评论 -
30/100. Queue Reconstruction by Height
class Solution(object): def reconstructQueue(self, people): """ :type people: List[List[int]] :rtype: List[List[int]] """ sortOf原创 2018-12-26 10:26:08 · 149 阅读 · 0 评论 -
19/100. Subtree of Another Tree
class Solution: def isSubtree(self, s, t): """ :type s: TreeNode :type t: TreeNode :rtype: bool """原创 2018-12-05 11:55:28 · 102 阅读 · 0 评论 -
11/100. Convert BST to Greater Tree
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def conv...原创 2018-11-10 09:36:38 · 121 阅读 · 0 评论 -
10/100. Majority Element
class Solution: def majorityElement(self, nums): """ :type nums: List[int] :rtype: int ""&原创 2018-12-29 10:55:56 · 129 阅读 · 0 评论 -
9/100. Reverse Linked List
非递归算法:# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def reverseList(self, head): ...原创 2018-11-09 12:07:29 · 77 阅读 · 0 评论 -
8/100. Find All Numbers Disappeared in an Array
class Solution: def findDisappearedNumbers(self, nums): """ :type nums: List[int] :rtype: List[int] ""&原创 2018-12-28 22:43:08 · 107 阅读 · 0 评论 -
7/100. Move Zeroes
class Solution: def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-pla原创 2018-11-09 10:07:16 · 127 阅读 · 0 评论 -
6/100. Invert Binary Tree
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def invertTree(s...原创 2018-11-09 10:03:48 · 65 阅读 · 0 评论 -
5/100. Single Number
class Solution: def singleNumber(self, nums): """ :type nums: List[int] :rtype: int ""&原创 2018-11-09 10:02:21 · 71 阅读 · 0 评论 -
4/100. 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 Solution: def maxDepth(sel...原创 2018-11-09 10:01:08 · 63 阅读 · 0 评论 -
1/100. Jewels and Stones
class Solution: def numJewelsInStones(self, J, S): """ :type J: str :type S: str :rtype: int &a原创 2018-11-08 17:10:21 · 154 阅读 · 0 评论 -
2/100. Hamming Distance
class Solution: def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int &原创 2018-11-08 17:36:23 · 146 阅读 · 0 评论 -
17/100. Maximum Subarray
class Solution: def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return None原创 2018-11-27 22:09:49 · 87 阅读 · 0 评论 -
18/100. Path Sum III
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def __init__(sel...原创 2018-12-04 22:13:26 · 82 阅读 · 0 评论 -
15/100. Climbing Stairs
class Solution: def climbStairs(self, n): """ :type n: int :rtype: int """ if n原创 2018-11-19 23:06:19 · 80 阅读 · 0 评论 -
14/100. Merge Two Sorted Lists
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def mergeTwoLists(self, l1, l2): """...原创 2018-11-19 22:45:00 · 79 阅读 · 0 评论 -
13/100. Best Time to Buy and Sell Stock
class Solution: def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if len(prices)<=1:原创 2018-11-19 21:44:24 · 80 阅读 · 0 评论 -
12/100. Diameter of Binary Tree
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def __init__(sel...原创 2018-11-19 20:45:26 · 98 阅读 · 0 评论 -
16/100. 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 isSymmetric(...原创 2018-11-20 14:54:07 · 94 阅读 · 1 评论 -
3/100. Merge Two Binary Trees
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def mergeTrees(s...原创 2018-11-09 10:00:24 · 93 阅读 · 0 评论