随笔分类 - leetcode
摘要:题目描述: 方法一: 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
阅读全文
浙公网安备 33010602011771号