[leetcode] Python(12)--字符串解码(394)、图像渲染(733)

从零开始的力扣(第十二天)~

1.字符串解码

给定一个经过编码的字符串,返回它解码后的字符串。

编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。

你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。

此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。

示例:
s = “3[a]2[bc]”, 返回 “aaabcbc”.
s = “3[a2[c]]”, 返回 “accaccacc”.
s = “2[abc]3[cd]ef”, 返回 “abcabccdcdcdef”.
—————————————————————————————————————————

将元素压入栈中,然后直接遍历所有情况
class Solution(object):
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
        if not s:
            return ""
        list_all = [i for i in s]
        stack = [0]
        while list_all:
            if list_all[0] in "0123456789":
                if type(stack[-1]) is int:
                    stack[-1] = (stack[-1] * 10 + int(list_all[0]))
                    list_all.pop(0)
                else:
                    stack.append(int(list_all[0]))
                    list_all.pop(0)
            else:
                if list_all[0] == "]":
                    temp = []
                    while stack[-1] != "[":
                        temp.insert(0,stack.pop())
                    temp = ''.join(i for i in temp)
                    stack.pop()
                    time = stack.pop()
                    stack.append(''.join(time * temp))
                    list_all.pop(0)
                else:
                    stack.append(list_all[0])
                    list_all.pop(0)
        stack.pop(0) if stack[0] == 0 else stack
        return ''.join(stack)

在这里插入图片描述

直接使用正则表达式
class Solution(object):
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
        while '[' in s:
            s = re.sub(r"(\d+)\[(\w+)\]",lambda m:int(m.group(1))*m.group(2),s)
        return s

在这里插入图片描述

2.图像渲染

有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。

给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像。

为了完成上色工作,从初始坐标开始,记录初始坐标的上下左右四个方向上像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应四个方向上像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为新的颜色值。

最后返回经过上色渲染后的图像。

示例 1:

输入:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
输出: [[2,2,2],[2,2,0],[2,0,1]]
解析:
在图像的正中间,(坐标(sr,sc)=(1,1)),
在路径上所有符合条件的像素点的颜色都被更改成2。
注意,右下角的像素没有更改为2,
因为它不是在上下左右四个方向上与初始点相连的像素点。
注意:

image 和 image[0] 的长度在范围 [1, 50] 内。
给出的初始点将满足 0 <= sr < image.length 和 0 <= sc < image[0].length。
image[i][j] 和 newColor 表示的颜色值在范围 [0, 65535]内。
—————————————————————————————————————————

使用广度优先遍历
class Solution:
    def floodFill(self, image, sr, sc, newColor):
        """
        :type image: List[List[int]]
        :type sr: int
        :type sc: int
        :type newColor: int
        :rtype: List[List[int]]
        """
        s = {(i, j) for i, row in enumerate(image) for j, col in enumerate(row) if col == image[sr][sc]}
        stack = [(sr, sc)]
        while stack:
            i, j = stack.pop()
            image[i][j] = newColor
            for item in (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1):
                if item in s:
                    s.remove(item)
                    stack.append(item)
        return image

在这里插入图片描述
以上就是今日经验!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值