随笔分类 - LeetCode
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def countPrimes(self, n: int) -> int: 3 nfilter = [False] * n 4 count = 0 5 for i in range(2,n): 6 if nfilter[i]:#非素
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def isHappy(self, n: int) -> bool: 3 s = set([n]) 4 while n != 1: 5 sn = str(n) 6 sums = 0 7 for i in range(len(sn))
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverse(self, x: int) -> int: 3 if x == 0: 4 return 0 5 operation = 1 6 if x < 0: 7 operation = -1 8 sx = str(x)
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def mySqrt(self, x: int) -> int: 3 return int(x ** 0.5)
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def trailingZeroes(self, n: int) -> int: 3 count = 0 4 while (n > 0): 5 count += n // 5 6 n = n // 5 7 return count
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def titleToNumber(self, s: 'str') -> 'int': 3 s = s.upper() 4 n = len(s) 5 sums = 0 6 k = 0 7 for i in range(n-1,-1,
阅读全文
摘要:1. 题目描述 2. 代码_解法1 1 class Solution: 2 def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: 3 """ 4 Do not return anything, mod
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def findUnsortedSubarray(self, nums: 'List[int]') -> int: 3 n = len(nums) 4 temp = sorted(nums) 5 start,end = 0,n-1
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def moveZeroes(self, nums: 'List[int]') -> None: 3 """ 4 Do not return anything, modify nums in-place instead. 5 """
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverseString(self, s: 'List[str]') -> None: 3 """ 4 Do not return anything, modify s in-place instead. 5 """ 6
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def removeDuplicates(self, nums: 'List[int]') -> int: 3 prevalue = 0 4 n = len(nums) 5 count = 0 6 i,j = 0,0 7 while
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def findContentChildren(self, g: 'List[int]', s: 'List[int]') -> int: 3 g.sort()#对g排序 4 s.sort()#对s排序 5 a, b = 0, 0#
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def maxProfit(self, prices: 'List[int]') -> int: 3 n = len(prices) 4 sums = 0 5 for i in range(1,n): 6 if prices[i]
阅读全文
摘要:1. 题目描述 2. 代码 1 import collections 2 3 class Solution: 4 def firstUniqChar(self, s: str) -> int: 5 dic = collections.OrderedDict()#有序字典 6 for i in ran
阅读全文
摘要:1. 题目描述 2. 代码 import collections class Solution: def intersect(self, nums1: 'List[int]', nums2: 'List[int]') -> 'List[int]': result = [] c1 = collecti
阅读全文
摘要:1. 题目描述 异位符指字母相同, 但次序不同. 2. 代码 1 class Solution: 2 def isAnagram(self, s: str, t: str) -> bool: 3 return sorted(s) == sorted(t) 思路: 利用sorted()进行排序, 然后
阅读全文
摘要:1. 题目描述 2.代码 1 class Solution: 2 def containsDuplicate(self, nums: List[int]) -> bool: 3 dic = {} 4 for n in nums: 5 if n not in dic: 6 dic[n] = 1 7 e
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def findDisappearedNumbers(self, nums: 'List[int]') -> 'List[int]': 3 dic = {} 4 maxvalue = len(nums) 5 result = []
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def majorityElement(self, nums: 'List[int]') -> int: 3 dic = {}#定义一个字典,key是数字,value是出现次数 4 length = len(nums)#计算列表的长
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def twoSum(self, nums: 'List[int]', target: int) -> 'List[int]': 3 s = {} 4 for i in range(len(nums)): 5 n = nums[i]
阅读全文