随笔分类 - leetcode
摘要:题目描述: 方法:动态规划 class Solution: def isValidPalindrome(self, s: str, k: int) -> bool: def isKPalRec(str1, str2, m, n): dp = [[0] * (n + 1) for _ in range
阅读全文
摘要:题目描述: 自己的提交:参考全排列 class Solution: def countSteppingNumbers(self, low: int, high: int) -> List[int]: a = [i for i in range(10)] res =[] def helper(num)
阅读全文
摘要:题目描述: 自己的提交: class Solution: def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool: if not root1 :return root_copy = root2 while
阅读全文
摘要:题目描述: 自己的提交: class Solution: def arraysIntersection(self, arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]: arr = arr1 + arr2 +arr3 res
阅读全文
摘要:题目描述: 方法:倒推 class Solution(object): def countVowelPermutation(self, n): MOD = 10 ** 9 + 7 a=e=i=o=u= 1 for ZZZ in xrange(n-1): a2,e2,i2,o2,u2 = e+i+u,
阅读全文
摘要:题目描述: 方法一:dfs class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: maxx = 0 R, C = len(grid), len(grid[0]) def dfs(r, c, visited, c
阅读全文
摘要:题目描述: class Solution: def longestSubsequence(self, arr: List[int], difference: int) -> int: dp = dict() for a in arr: pre = a - difference if pre in d
阅读全文
摘要:题目描述: 自己的提交: class Solution: def minCostToMoveChips(self, chips: List[int]) -> int: res = float('inf') nums = set(chips) for num in nums: ans = 0 for
阅读全文
摘要:题目描述; 方法一:O(N) O(1) # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution:
阅读全文
摘要:题目描述: 方法一:双栈 class MyQueue: def __init__(self): """ Initialize your data structure here. """ from collections import deque self.stack = deque() def pu
阅读全文
摘要:题目描述: 第一次提交: class Solution: def isPowerOfTwo(self, n: int) -> bool: if n==0:return False if n==1: return True s = str(bin(n))[3:] for i in s: if i ==
阅读全文
摘要:题目描述: 方法一:递归: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None cl
阅读全文
摘要:题目描述: 方法一:摩尔投票法 class Solution: def majorityElement(self, nums: List[int]) -> List[int]: candiate1 = candiate2 = None cnt1 = cnt2 = 0 for num in nums:
阅读全文
摘要:题目描述: 第一次提交:双指针 O(N) class Solution: def summaryRanges(self, nums: List[int]) -> List[str]: #first = 0 last = 0 res = [] while last!=len(nums): first
阅读全文
摘要:题目描述: 方法一:中缀转后缀 #!_*_coding:utf-8_*_ class Solution: def calculate(self, s: str) -> int: def in_to_suffix(s): priority = {'+': 1, '-': 1, '*': 2, '/':
阅读全文
摘要:题目描述: from collections import deque class MyStack: def __init__(self): """ Initialize your data structure here. """ self.queue = deque() def push(self
阅读全文
摘要:题目描述: 方法一:中缀表达式转后缀表达式,再用150题方法求解 class Solution: def calculate(self, s: str) -> int: tokens = self.infix_to_suffix(s) result = self.evalRPN(tokens) re
阅读全文
摘要:题目描述: 方法: class Solution: def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: # 调整两个矩形位置, 让第一个矩形靠最左边 if A >
阅读全文
摘要:题目描述: 方法一:O(N) class Solution: def countNodes(self, root: TreeNode) -> int: if not root: return 0 return 1 + self.countNodes(root.left) + self.countNo
阅读全文
摘要:题目描述: 方法一:动态规划 class Solution: def maximalSquare(self, matrix: List[List[str]]) -> int: if not matrix: return 0 row = len(matrix) col = len(matrix[0])
阅读全文
浙公网安备 33010602011771号