栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python Leetcode

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Leetcode

1、223. 矩形面积

知识点: max,min

class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        a = (ax1 - ax2) * (ay1 - ay2)
        b = (bx2 - bx1) * (by2 - by1) 
        w = min(ax2, bx2) - max(ax1, bx1)
        h = min(ay2, by2) - max(ay1, by1)
        c = max(w, 0) * max(h, 0)
        
        return a + b - c
2、414. 第三大的数
class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        x = y = z = -inf
        for n in nums:
            # if n > x:
            #     x, n = n, x # 替换出 x
            # if x > n > y:
            #     y, n = n, y
            # if y > n > z:
            #     z = n

            if n > x:
                z, y, x = y, x, n  # 依次替换
            elif x > n > y:
                z, y = y, n              
            elif y > n > z:
                z = n                

        return x if z == -inf else z
列表(list) 集合(set) 字典(dict) 排序(sorted,sort)
    # return x[-3] if len(x:=sorted(list(set(nums)))) > 2 else max(x)
class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        d = {}
        for n in nums:
            d[n] = d.get(n, 0) + 1
        return max(d) if len(d) < 3 else sorted(d)[-3]
                
        # return x[-3] if len(x:=sorted(list(set(nums)))) > 2 else max(x)
3、575. 分糖果

知识点: set.add

class Solution:
    def distributeCandies(self, candyType: List[int]) -> int:        
        n = len(candyType)//2
        s = set()
        for c in candyType:
            s.add(c)
        
        return len(s) if len(s) <= n else n
        
        #return min(len(candyType)//2, len(set(candyType)))            
4、283. 移动零 方法一:双指针

使用双指针,左指针指向当前已经处理好的序列的尾部,右指针指向待处理序列的头部。

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        left, n = 0, len(nums)
        
        for i in range(n):
            if nums[i]:
                nums[i], nums[left] = nums[left], nums[i]
                left += 1

        return nums
方法二:pop,append
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        # for i in range(len(nums)):
        #     if nums[i] == 0:
        #         nums.pop(i)
        #         nums.append(0)
        # return nums
        # 如:[0,0,1] 输出 [0,1,0]

        i, j = 0, len(nums)-1
        while i < j:
            if nums[i] == 0:
                nums.pop(i)
                nums.append(0)
                j -= 1
            else:
                i += 1
        return nums
5、412. Fizz Buzz
class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        res = []
        for i in range(1, n+1):
            # if not i%15:
            #     tmp = "FizzBuzz"
            # elif not i%3:
            #     tmp = "Fizz"
            # elif not i%5:
            #     tmp = "Buzz"
            # else:
            #     tmp = str(i)

            tmp = ""
            if not i%3:
                tmp += "Fizz"
            if not i%5:
                tmp += "Buzz"
            if not tmp:
                tmp = str(i)
            
            res.append(tmp)
        
        return res
        
        # return ["Fizz"[i%3*4:] + "Buzz"[i%5*4:] or str(i) for i in range(1,n+1)]
6、258. 各位相加
class Solution:
    def addDigits(self, num: int) -> int:
        return (num-1)%9 + 1 if num else 0
268. 丢失的数字
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        nums.sort()
        n = len(nums)
        if n == nums[-1] + 1:return n
        for i, e in enumerate(nums):
            if i != e:
                return i
263. 丑数
class Solution:
    def isUgly(self, n: int) -> bool:
        if n <= 0: return False
        for factor in [2,3,5]:
            while not n % factor:
                n //= factor
        
        return n == 1
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/293514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号