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