07 2019 档案
摘要:方法一: class Solution(object): def minimumSemesters(self, N, relations): """ :type N: int :type relations: List[List[int]] :rtype: int """ dic = {} dic2
阅读全文
摘要:方法一: class Solution: def minimumCost(self, N: int, conections: List[List[int]]) -> int: def find(i): if father[i]==i: return i else: father[i]=find(fa
阅读全文
摘要:第一次提交: class Solution: def isArmstrong(self, N: int) -> bool: n = N l = len(str(N)) res = 0 while N: a = N % 10 res += a**l N = N//10 if res == n: ret
阅读全文
摘要:第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: if i not in dict: dict[i] = 1 else: dict[i] += 1 res
阅读全文
摘要:题目描述: 方法一:dfs class Solution: def numIslands(self, grid: List[List[str]]) -> int: def dfs(grid,r,c): nr = len(grid) nc = len(grid[0]) if r<0 or c<0 or
阅读全文
摘要:题目描述: class Solution: def maxAbsValExpr(self, arr1, arr2) -> int: def function(s1,s2): result1=[] result2=[] result3=[] result4=[] for i in range(len(
阅读全文
摘要:题目描述: class Solution(object): def mctFromLeafValues(self, arr): """ :type arr: List[int] :rtype: int """ n = len(arr) f = {1: [0] * n} for l in range(
阅读全文
摘要:恢复内容开始 题目描述: class Solution: def shortestAlternatingPaths(self, n: int, red_edges, blue_edges): def function(n,r,b): result=[[float("inf")]*2 for _ in
阅读全文
摘要:题目描述: 方法一: class Solution(object): def numEquivDominoPairs(self, dominoes): """ :type dominoes: List[List[int]] :rtype: int """ f = {} ret = 0 for d i
阅读全文
摘要:题目描述: 第一次提交:BFS O(N) O(N) # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None #
阅读全文
摘要:题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right =
阅读全文
摘要:题目描述: 方法一: class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ def dp(nums): pre = 0 cur = 0 for i in nums: temp =
阅读全文
摘要:题目描述: 方法一:O(N) O(N) class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ bp = [0] * (len(nums) + 2) for i in range(l
阅读全文
摘要:题目描述; 第一次提交: class Solution(object): def hammingWeight(self, n): """ :type n: int :rtype: int """ n = str(bin(n)) count = 0 for i in n: if i == '1': c
阅读全文
摘要:题目描述: 方法一:内置函数 class Solution: # @param n, an integer # @return an integer def reverseBits(self, n): return int(bin(n)[2:].zfill(32)[::-1],2) 方法二:位运算
阅读全文
摘要:题目描述: 方法一: class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ for i in r
阅读全文
摘要:题目描述: 方法一: class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: n = 10 d = {} for i in range(len(s)-9): if s[i:i+10] in d: d[s[i:i
阅读全文
摘要:题目描述: 方法一: class LargerNumKey(str): def __lt__(x, y): return x+y > y+x class Solution: def largestNumber(self, nums): largest_num = ''.join(sorted(map
阅读全文
摘要:题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class
阅读全文
摘要:题目描述: 方法一: class Solution: def fractionToDecimal(self, numerator: int, denominator: int) -> str: if numerator == 0: return "0" res = [] if (numerator
阅读全文
摘要:题目描述: 方法一: class Solution: def compareVersion(self, version1: str, version2: str) -> int: v1, v2 = ([*map(int, v.split('.'))] for v in (version1, vers
阅读全文
摘要:题目描述: 二分法: class Solution: def findPeakElement(self, nums: List[int]) -> int: l, h = 0, len(nums) - 1 while l <= h: m = (l + h) // 2 if (not m or nums
阅读全文
摘要:题目描述: 方法一:辅助栈 class MinStack: def __init__(self): """ initialize your data structure here. """ self.stack = [] self.min_stack = [] def push(self, x: i
阅读全文
摘要:题目描述: 方法一: class Solution: def findMin(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if
阅读全文
摘要:题目描述: 第一次提交:O(N) class Solution: def findMin(self, nums: List[int]) -> int: if not nums: return l,r = 0,len(nums)-1 while l<r: if nums[l]>nums[r]: l +
阅读全文
摘要:题目描述: 方法一:动态规划 class Solution: def maxProduct(self, nums: List[int]) -> int: ret,up,down=nums[0],nums[0],nums[0] for n in nums[1:]: if n>=0: up,down=m
阅读全文
摘要:题目描述: 方法一: return " ".join(s.split()[::-1]) 方法二: class Solution: def reverseWords(self, s: str) -> str: res = re.findall('\S+',s) return " ".join(res[
阅读全文
摘要:题目描述: 方法一: class Solution: def evalRPN(self, t: List[str]) -> int: d=[] n=len(t) for i in range(n): if '0'<=t[i][-1]<='9': d+=[t[i]] else: d[-2]=str(i
阅读全文
摘要:题目描述: 方法一:快排 class Solution: def sortList(self, head: ListNode) -> ListNode: def partition(start, end): node = start.next.next pivotPrev = start.next
阅读全文
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def inser
阅读全文
摘要:题目描述: 方法一:有序字典 O(1) from collections import OrderedDict class LRUCache(OrderedDict): def __init__(self, capacity: int): self.capacity = capacity def g
阅读全文
摘要:恢复内容开始 题目描述: 方法一:层次遍历 """ # Definition for a Node. class Node: def __init__(self, val, left, right, next): self.val = val self.left = left self.right
阅读全文
摘要:题目描述: 方法一: class Solution(object): def reorderList(self, head): """ :type head: ListNode :rtype: None Do not return anything, modify head in-place ins
阅读全文
摘要:题目描述: 方法一:O(n) O(n) # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class So
阅读全文
摘要:题目描述: 方法一:动态规划 O(n^2) O(n) class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: maxlen=0 for word in wordDict: if len(word)>maxle
阅读全文
摘要:题目描述: 方法一:dfs O(N) O(N) class Solution: def copyRandomList(self, head: 'Node') -> 'Node': def dfs(head): if not head: return None if head in visited:
阅读全文
摘要:题目描述: 方法一:数学 class Solution: def singleNumber(self, nums: List[int]) -> int: return (sum(set(nums))*3 - sum(nums))//2 方法二: class Solution: def singleN
阅读全文
摘要:题目描述: 方法一:哈希表 O(N) O(N) class Solution: def singleNumber(self, nums: List[int]) -> int: hash_table = {} for i in nums: try: hash_table.pop(i) except:
阅读全文
摘要:题目描述: 方法一:O(n^2) (超时) class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: for i in range(len(gas)): if gas[i]-cost[i
阅读全文
摘要:题目描述: 方法一:dfs+hashmap """ # Definition for a Node. class Node: def __init__(self, val, neighbors): self.val = val self.neighbors = neighbors """ class
阅读全文
摘要:题目描述: 第一次提交: class Solution: def partition(self, s: str) -> List[List[str]]: res = [] temp = [] def backtrack(s,temp): if not s: res.append(temp) for
阅读全文
摘要:题目描述: 方法一:dfs class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if not b
阅读全文
摘要:题目描述: 第一次提交: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None cla
阅读全文
摘要:题目描述: 官方题解:双向bfs O(mn) O(mn) from collections import defaultdict class Solution: def __init__(self): self.length = 0 self.all_combo_dict = defaultdict
阅读全文
摘要:题目描述: 方法一:正则 class Solution: def isPalindrome(self, s: str) -> bool: return ''.join(re.findall('\w*',s)).lower() == ''.join(re.findall('\w*',s)).lower
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int], fee: int) -> int: n = len(prices) dp_i_0 = 0 dp_i_1 = float('-inf') for i in range(0
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: n = len(prices) dp_i_0 = 0 dp_i_1 = float('-inf') dp_pre_0 = 0 for i in rang
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: if len(prices) <= 1: return 0 if (k < len(prices) // 2) : dp = [[-pr
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: dp_i1_0 = 0 dp_i1_1 = float('-inf') dp_i2_0 = 0 dp_i2_1 = float('-inf') for
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 for i in range(1,len(prices)): tem = prices[i] - prices[i-1] if t
阅读全文
摘要:题目描述; 方法一:O(n) O(1) class Solution: def maxProfit(self, prices: List[int]) -> int: minprices = float('inf') maxprofit = 0 for i in prices: if i<minpri
阅读全文
摘要:题目描述: 第一次提交:动态规划 class Solution: def minimumTotal(self, triangle: List[List[int]]) -> int: n = len(triangle) for i in range(n-2,-1,-1): for j in range
阅读全文
摘要:题目描述: 第一次提交: class Solution: def getRow(self, rowIndex: int) -> List[int]: k = rowIndex pre = [1] + [0] * k res = [1] + [0] * k for i in range(1,k+1):
阅读全文
摘要:题目描述: 输入:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","l
阅读全文
摘要:题目描述: 方法一:迭代 class Solution: def flatten(self, root: TreeNode) -> None: """ Do not return anything, modify root in-place instead. """ cur = root while
阅读全文
摘要:恢复内容开始 题目描述: 方法一:O(n) O(n) class Solution: def sortedListToBST(self, head: ListNode) -> TreeNode: nums = [] while head: nums.append(head.val) head = h
阅读全文
摘要:题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: assert len(inorder)==len(postorder) if
阅读全文
摘要:题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class
阅读全文
摘要:题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class
阅读全文
摘要:题目描述: 方法一:递归O(n) O(n) # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right =
阅读全文
摘要:题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None cla
阅读全文
摘要:题目描述: 方法一:动态规划 O(n^2) O(n) class Solution: def numTrees(self, n: int) -> int: dp = [0]*(n+1) dp[0],dp[1] = 1,1 for i in range(2,n+1): for j in range(1
阅读全文
摘要:题目描述: 方法一:递归 class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return helper(root.lef
阅读全文
摘要:恢复内容开始 题目描述: 方法一:递归 class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return res.appen
阅读全文
摘要:题目描述: 方法一:迭代 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: res,stack = [],[] while True: while root: stack.append(root) roo
阅读全文
摘要:题目描述: 方法一:暴力法 class Solution: def restoreIpAddresses(self, s: str) -> List[str]: n = len(s) res = [] # 判读是否满足ip的条件 def helper(tmp): if not tmp or (tmp
阅读全文
摘要:题目描述: 方法一: class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(0) dummy.next = head pre = dummy for
阅读全文
摘要:恢复内容开始 题目描述: 方法一:回溯(超时) class Solution: def numDecodings(self, s: str) -> int: res = 0 def backtrack(s): nonlocal res if not s: res += 1 return if int
阅读全文
摘要:题目描述: 方法一: class Solution: def grayCode(self, n: int) -> List[int]: gray = [0] for i in range(n): add = 2**i for j in range(len(gray)-1,-1,-1): gray.a
阅读全文
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def parti
阅读全文
摘要:题目描述: 33题 方法一: class Solution: def search(self, nums: List[int], target: int) -> bool: l, r = 0, len(nums) - 1 while l <= r: m = (l+r) // 2 if target
阅读全文
摘要:题目描述: 第一次提交: class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] def backtrack(i,temp_list): if len(temp_list)==k: res.appe
阅读全文
摘要:题目描述: 方法一: class Solution: def sortColors(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ p0 = curr = 0
阅读全文
摘要:题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums: List[int]) -> int: nums.reverse() for i in range(len(nums)-3,-1,-1): if nums[i]==nums[i+
阅读全文
摘要:题目描述: 方法一:二分 class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m = len(matrix) if m==0: return False n = len(matri
阅读全文
摘要:题目描述: 方法一:O(mn) O(1) class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead.
阅读全文
摘要:题目描述: 方法: class Solution: def simplifyPath(self, path: str) -> str: stack = [] path = path.split("/") for item in path: if item == "..": if stack : st
阅读全文
摘要:题目描述: 第一次提交:动态规划 class Solution: def minPathSum(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) for i in range(1,m): grid[i][0] =
阅读全文
摘要:题目描述: 第一次提交: class Solution: def uniquePathsWithObstacles(self, obstacleGrid) : m = len(obstacleGrid) n = len(obstacleGrid[0]) for i in range(m): for
阅读全文
摘要:题目描述: 方法一: class Solution: def uniquePaths(self, m: int, n: int) -> int: return int(math.factorial(m+n-2)/math.factorial(m-1)/math.factorial(n-1)) 方法二
阅读全文
摘要:题目描述: 方法一; # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def rotat
阅读全文
摘要:题目描述: 方法一: class Solution: import math def getPermutation(self, n: int, k: int) -> str: result = '' mod = k-1 if n==1:return '1' n_set = [str(i) for i
阅读全文
摘要:题目描述: 方法一:O(nlogn) class Solution: def merge(self, intervals: List[List[int]]) -> List[List[int]]: intervals.sort(key=lambda intervals:intervals[0]) m
阅读全文
摘要:题目描述: 方法一:O(logn)递归: class Solution: def myPow(self, x: float, n: int) -> float: if n == 0: return 1 if n < 0: return 1/self.myPow(x,-n) if n&1: retur
阅读全文
摘要:题目描述: 方法一: import collections class Solution: def groupAnagrams(self, strs) : ans = collections.defaultdict(list) for s in strs: ans[tuple(sorted(s))]
阅读全文
摘要:题目描述: 方法一:先转置再反转 class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ n
阅读全文
摘要:题目描述: 第一次提交: class Solution: def addStrings(self, num1: str, num2: str) -> str: if len(num1) < len(num2): num1,num2 = num2,num1 num1 = [_ for _ in num
阅读全文
摘要:恢复内容开始 题目描述: 方法一:O(n2) class Solution: def multiply(self, num1: str, num2: str) -> str: def str2int(s): return ord(s) - ord("0") res = 0 num1,num2 = n
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: nums.sort() if not nums: return [] n = len(nums) res = [] d
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: if not sum: return [] res = [] n = len(nums) def backtrack(idx,tem
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: #nums = nums.sort() n = len(nums) res = [] def backtrack(num
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def permute(self, nums): n = len(nums) res = [] def helper2(nums, temp_list, length): if length == n: res.append(temp_lis
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: candidates.sort() res = [] def backtrac
阅读全文
摘要:题目描述: 方法一:O(1) O(1) class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: rows = [{} for i in range(9)] columns = [{} for i in rang
阅读全文
摘要:题目描述: 方法一: class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: if len(nums)==0: return[-1,-1] ans = [-1]*2 begin,end = 0
阅读全文
摘要:题目描述: 方法一: class Solution: def search(self, nums: List[int], target: int) -> int: def half_search(nums,target,i,j,head): mid = int(0.5*(j+i)) if i>j:
阅读全文
摘要:题目描述: 方法一:O(n) O(1) class Solution: def nextPermutation(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """
阅读全文
摘要:题目描述: 方法一: class Solution: def divide(self, dividend: int, divisor: int) -> int: res = 0 sign = 1 if dividend ^ divisor >= 0 else -1 divd = abs(divide
阅读全文
摘要:题目描述: 方法一:创建新节点(超时) class Solution: def swapPairs(self, head: ListNode) -> ListNode: dummy=ListNode(0) p = dummy h = head while h: if h and h.next: p.
阅读全文
摘要:题目描述: 方法一:暴力 class Solution(object): def generateParenthesis(self, n): def generate(A = []): if len(A) == 2*n: if valid(A): ans.append("".join(A)) els
阅读全文
摘要:题目描述: 方法一:快慢指针 class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: if not head: return dummy = ListNode(0) dummy.next = he
阅读全文
摘要:恢复内容开始 题目描述: 方法一: class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: n = len(nums) if n<4: return [] nums.sort() res
阅读全文
摘要:题目描述: 方法一: class Solution: def intToRoman(self, num: int) -> str: res = "" values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] symbols = [
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ phone = {'2': ['a', 'b', 'c'], '3': ['d
阅读全文
摘要:题目描述: 方法一:排序+双指针 class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: nums.sort() res=float("inf") for k in range(len(nums)
阅读全文
摘要:题目描述; 方法一: class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res = [] nums.sort() for k in range(len(nums)-2): if nums[k]>0:brea
阅读全文
摘要:题目描述: 方法一:双指针 class Solution: def maxArea(self, height: List[int]) -> int: left = 0 right = len(height)-1 area = 0 while left<right: cur = min(height[
阅读全文
摘要:题目描述: 方法一:正则 import re class Solution: def myAtoi(self, str: str) -> int: INT_MAX = 2149483647 INT_MIN = -2147483648 num = re.findall(r'^[\+\-]?\d+',s
阅读全文
摘要:题目描述: 方法一: class Solution: def convert(self, s: str, numRows: int) -> str: if not s: return "" if numRows == 1: return s s_Rows = [""] * numRows i = 0
阅读全文
摘要:方法一:动态规划 O(n2) O(n2) class Solution: def longestPalindrome(self, s: str) -> str: size = len(s) if size <= 1: return s # 二维 dp 问题 # 状态:dp[l,r]: s[l:r]
阅读全文
摘要:题目描述: 方法一:O(N) class Solution: def lengthOfLongestSubstring(self, s: str) -> int: slow = 0 fast = 0 res_max = 0 table = dict() while slow<len(s) and f
阅读全文
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTw
阅读全文
浙公网安备 33010602011771号