LeetCode笔记:Biweekly Contest 58(补发)

1. 题目一

给出题目一的试题链接如下:

1. 解题思路

这一题没啥,就是当满足与前一个字符不同或者前一个字符连续出现的个数不多于2时就保留该字符,否则跳过,然后重组字符即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def makeFancyString(self, s: str) -> str:
        res = []
        cnt = 0
        for c in s:
            if res == [] or res[-1] != c:
                cnt = 1
                res.append(c)
            elif cnt < 2:
                cnt += 1
                res.append(c)
        return "".join(res)

提交代码评测得到:耗时716ms,占用内存16.1MB。

2. 题目二

给出题目二的试题链接如下:

1. 解题思路

这题也还好,就是按照题目的定义进行一下验证就行了。

2. 代码实现

给出python代码实现如下:

class Solution:
    def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
        delta = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1,1), (-1,-1)]
        for dx, dy in delta:
            r, c = rMove + dx, cMove + dy
            if not 0 <= r < 8 or not 0 <= c < 8:
                continue
            pad = "B" if color == "W" else "W"
            if board[r][c] != pad:
                continue
            while 0 <= r < 8 and 0 <= c < 8 and board[r][c] == pad:
                r += dx
                c += dy
            if 0 <= r < 8 and 0 <= c < 8 and board[r][c] == color:
                return True
        return False

提交代码评测得到:耗时128ms,占用内存14.3MB。

3. 题目三

给出题目三的试题链接如下:

这一题基本就是按照答案的思路做了一下,这里就不越俎代庖多做解说了,直接给出官方的解法思路如下:

4. 题目四

给出题目四的试题链接如下:

这题放弃了,不想折腾了,唉……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值