python leetcode 401-410

# 401
class Solution:
    def readBinaryWatch(self, num: int) -> List[str]:
        res = []
        for h in range(12):
            for m in range(60):
                if bin(h).count("1") + bin(m).count("1") == num:
                    res.append("{}:{:02d}".format(h, m))
        return res
# 402
class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        stack = []
        remain = len(num) - k
        for digit in num:
            while k and stack and stack[-1] > digit:
                stack.pop()
                k -= 1
            stack.append(digit)
        return ''.join(stack[:remain]).lstrip('0') or '0'
# 403
class Solution:
    def canCross(self, stones: List[int]) -> bool:
        end = stones[-1]
        visited = defaultdict(bool)     #记忆化+dfs
        stones_set = set(stones)        #为了查找更快
        def dfs(start , pre_step):
            if start == end:
                return True
            if (start, pre_step) in visited:
                return visited[(start, pre_step)]
            for cur_step in (pre_step - 1, pre_step, pre_step + 1):
                if cur_step <= 0:    #如果向左了,或者原地了
                    continue
                nxt_start = start + cur_step #下一步的开始(起跳点)
                if nxt_start in stones_set and dfs(nxt_start, cur_step) == True:    #下一步踩在石头上,且后面能到终点
                    visited[(start, pre_step)] = True
                    return True
            visited[(start, pre_step)] = False
            return False
        return dfs(stones[0], 0)
# 404
class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        self.res = 0
        def helper(root):
            if not root:
                return 0
            if root.left and not root.left.left and not root.left.right:
                self.res = self.res + root.left.val
            helper(root.left)
            helper(root.right)
            return self.res
        tmp = root
        helper(tmp)
        return self.res
# 405
class Solution:
    def toHex(self, num: int) -> str:
        if num == 0: return '0'
        res = ''
        if num < 0:
            num = (abs(num) ^ ((2 ** 32) - 1)) + 1
        while num >= 1:
            b , a = divmod(num , 16)
            num = b
            if a == 10:
                a = 'a'
            elif a == 11:
                a = 'b'
            elif a == 12:
                a = 'c'
            elif a == 13:
                a = 'd'
            elif a == 14:
                a = 'e'
            elif a == 15:
                a = 'f'
            res = str(a) + res
        return res
# 408
class Solution:
    def validWordAbbreviation(self, word: str, abbr: str) -> bool:
        num = 0
        p = 0
        m = len(word)
        for a in abbr:
            if a.isdigit():
                if num == 0 and a == "0": return False
                num = int(a) + 10 * num
            else:
                p += num
                if p >= m or word[p] != a: return False
                num = 0
                p += 1
        return p + num == len(word)
# 409
class Solution:
    def longestPalindrome(self, s: str) -> int:
        s = Counter(s)
        res = 0
        flag = 0
        for i in s.values():
            if i % 2 == 0:
                res += i
            else:
                res += i -1
                flag = 1
        return res + flag

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值