自学内容网 自学内容网

Python | Leetcode Python题解之第482题秘钥格式化

题目:

题解:

class Solution:
    def licenseKeyFormatting(self, s: str, k: int) -> str:
        ans = list()
        cnt = 0

        for i in range(len(s) - 1, -1, -1):
            if s[i] != "-":
                ans.append(s[i].upper())
                cnt += 1
                if cnt % k == 0:
                    ans.append("-")
        
        if ans and ans[-1] == "-":
            ans.pop()
        
        return "".join(ans[::-1])

原文地址:https://blog.csdn.net/Mopes__/article/details/142968479

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!