每日leetcode4.12-盛水(双指针)+最大数(排序)

每日leetcode4.12-盛水(双指针)+最大数(排序)

11. 盛最多水的容器

典型双指针

from typing import List


class Solution:
    def maxArea(self, height: List[int]) -> int:
        i = 0
        j = len(height) - 1
        ans = 0
        while i < j:
            v = 0
            if height[i] < height[j]:
                v = height[i] * (j - i)
                i += 1
            else:
                v = height[j] * (j - i)
                j -= 1
            if v > ans:
                ans = v
        return ans


if __name__ == '__main__':
    x = Solution()
    s = [1, 8, 6, 2, 5, 4, 8, 3, 7]
    print(x.maxArea(s))

179. 最大数

注意处理0的情况,其次py中排序方法是利用关键字的大小,py3中不再有cmp

from typing import List


class Solution:
    def largestNumber(self, nums: List[int]) -> str:
        if sum(nums) == 0:
            return "0"

        def cmp(x):
            if x == 0:
                return 0
            v = []
            l = 0
            while x:
                v.append(x % 10)
                x //= 10
                l += 1
            ans = 0
            for i in range(9):
                ans = ans * 10 + v[l - 1 - i % l]
            return ans

        ans = ""
        nums.sort(key=cmp, reverse=True)
        for i in nums:
            ans += str(i)
        return ans


if __name__ == '__main__':
    x = Solution()
    s = [0]
    print(x.largestNumber(s))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值