随笔分类 - leetcode
摘要:恢复内容开始 题目描述: 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[
阅读全文
浙公网安备 33010602011771号