10 2020 档案
摘要:1. 题目描述 注: 回溯法 2. 代码 1 class Solution: 2 def generateParenthesis(self, n: int) -> List[str]: 3 y = list() 4 string = '(' 5 L = 1 6 R = 0 7 self.BackTr
阅读全文
摘要:1. 题目描述 注: 属于动态规划, 保存已解决的子问题的答案,在需要时再找出已求得的答案, 这样就可以避免大量的重复计算, 节省时间. 可以用一个表来记录所有已解的子问题的答案, 不管该子问题以后是否被用到, 只要它被计算过, 就将其结果填入表中. 2. 代码 1 class Solution:
阅读全文
摘要:1. 题目描述 2. 代码 1 import itertools 2 class Solution: 3 def permute(self, nums: List[int]) -> List[List[int]]: 4 n = len(nums) 5 temp = itertools.permuta
阅读全文
摘要:1. 题目描述 注: 动态规划 2. 代码 1 import sys 2 class Solution: 3 def maxProfit(self, prices: 'List[int]') -> int: 4 n = len(prices) 5 maxval = 0 6 minval = sys.
阅读全文
摘要:1. 题目描述 注: 动态规划 2. 代码 1 class Solution: 2 def climbStairs(self, n: int) -> int: 3 if n <= 2: 4 return n 5 else: 6 dp = [0] * (n+1) 7 dp[1] = 1 8 dp[2]
阅读全文
摘要:1. 题目描述 注: 属于动态规划. 2. 代码 1 class Solution: 2 def maxSubArray(self, nums: List[int]) -> int: 3 for i in range(1,len(nums)): 4 nums[i] = max(nums[i-1] +
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def isPowerOfThree(self, n: int) -> bool: 3 while n > 2 and n % 3 == 0: 4 n = n // 3 5 return n == 1
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def countPrimes(self, n: int) -> int: 3 nfilter = [False] * n 4 count = 0 5 for i in range(2,n): 6 if nfilter[i]:#非素
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def isHappy(self, n: int) -> bool: 3 s = set([n]) 4 while n != 1: 5 sn = str(n) 6 sums = 0 7 for i in range(len(sn))
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverse(self, x: int) -> int: 3 if x == 0: 4 return 0 5 operation = 1 6 if x < 0: 7 operation = -1 8 sx = str(x)
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def mySqrt(self, x: int) -> int: 3 return int(x ** 0.5)
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def trailingZeroes(self, n: int) -> int: 3 count = 0 4 while (n > 0): 5 count += n // 5 6 n = n // 5 7 return count
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def titleToNumber(self, s: 'str') -> 'int': 3 s = s.upper() 4 n = len(s) 5 sums = 0 6 k = 0 7 for i in range(n-1,-1,
阅读全文
摘要:1. 题目描述 2. 代码_解法1 1 class Solution: 2 def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: 3 """ 4 Do not return anything, mod
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def findUnsortedSubarray(self, nums: 'List[int]') -> int: 3 n = len(nums) 4 temp = sorted(nums) 5 start,end = 0,n-1
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def moveZeroes(self, nums: 'List[int]') -> None: 3 """ 4 Do not return anything, modify nums in-place instead. 5 """
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverseString(self, s: 'List[str]') -> None: 3 """ 4 Do not return anything, modify s in-place instead. 5 """ 6
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def removeDuplicates(self, nums: 'List[int]') -> int: 3 prevalue = 0 4 n = len(nums) 5 count = 0 6 i,j = 0,0 7 while
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def findContentChildren(self, g: 'List[int]', s: 'List[int]') -> int: 3 g.sort()#对g排序 4 s.sort()#对s排序 5 a, b = 0, 0#
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def maxProfit(self, prices: 'List[int]') -> int: 3 n = len(prices) 4 sums = 0 5 for i in range(1,n): 6 if prices[i]
阅读全文
摘要:1. 题目描述 2. 代码 1 import collections 2 3 class Solution: 4 def firstUniqChar(self, s: str) -> int: 5 dic = collections.OrderedDict()#有序字典 6 for i in ran
阅读全文
摘要:1. 题目描述 2. 代码 import collections class Solution: def intersect(self, nums1: 'List[int]', nums2: 'List[int]') -> 'List[int]': result = [] c1 = collecti
阅读全文
摘要:1. 题目描述 异位符指字母相同, 但次序不同. 2. 代码 1 class Solution: 2 def isAnagram(self, s: str, t: str) -> bool: 3 return sorted(s) == sorted(t) 思路: 利用sorted()进行排序, 然后
阅读全文
摘要:1. 题目描述 2.代码 1 class Solution: 2 def containsDuplicate(self, nums: List[int]) -> bool: 3 dic = {} 4 for n in nums: 5 if n not in dic: 6 dic[n] = 1 7 e
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def findDisappearedNumbers(self, nums: 'List[int]') -> 'List[int]': 3 dic = {} 4 maxvalue = len(nums) 5 result = []
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def majorityElement(self, nums: 'List[int]') -> int: 3 dic = {}#定义一个字典,key是数字,value是出现次数 4 length = len(nums)#计算列表的长
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def twoSum(self, nums: 'List[int]', target: int) -> 'List[int]': 3 s = {} 4 for i in range(len(nums)): 5 n = nums[i]
阅读全文
摘要:1. 题目描述 2. 代码 1 class MinStack: 2 def __init__(self): 3 self.stack = [] 4 self.min_stack = [] 5 6 def push(self, x: int) -> None: 7 self.stack.append(
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def isValid(self, s: str) -> bool: 3 n = len(s)#获取字符串的长度 4 if n == 0:#如果长度为0,则表示空串,本题认为是合法的串 5 return True 6 if n %
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverseList(self, head: ListNode) -> ListNode: 3 temp = ListNode(-1) 4 while head != None: 5 nextnode = head.nex
阅读全文
摘要:1. 题目描述 2. 代码 1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, val=0, next=None): 4 # self.val = val 5 # self.next =
阅读全文