解压缩RLE算法

发布于 2025-01-13 21:49:22 字数 134 浏览 3 评论 0原文

在此处输入图像描述

有谁知道如何解压缩 RLE 算法,但输入必须以字母开头,例如“A2B5” 。我的程序的输入以数字开头,例如“2B5B”。

enter image description here

Does anyone know how to decompress the RLE algorithm but the input must start with a letter for example "A2B5". The input from my program starts with a number, for example "2B5B".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2025-01-20 21:49:23

这是算法的实现,假设仅复制单个字母并支持多个数字。

compressed = "A10B5"

def is_digit(s):
    try:
        int(s)
        return True
    except:
        return False

def decompress(c):
    result = ""
    ch = ''
    count = 0
    for i in c:
        if is_digit(i):
            count = count * 10 + int(i)
        else:
            result += ch * count
            count = 0
            ch = i
    result += ch * count
    return result

print(decompress(compressed))

Here is an implementation of the algorithm assuming only single letters are to be replicated and multiple digits are to be supported.

compressed = "A10B5"

def is_digit(s):
    try:
        int(s)
        return True
    except:
        return False

def decompress(c):
    result = ""
    ch = ''
    count = 0
    for i in c:
        if is_digit(i):
            count = count * 10 + int(i)
        else:
            result += ch * count
            count = 0
            ch = i
    result += ch * count
    return result

print(decompress(compressed))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文