首页 > 其他 > 详细

[leetcode]License Key Formatting

时间:2020-01-31 11:24:30      阅读:72      评论:0      收藏:0      [点我收藏+]

注意python的字符和数字转化函数是chr和ord

class Solution:
    def licenseKeyFormatting(self, S: str, K: int) -> str:
        lst = []
        for c in S:
            if c == ‘-‘:
                continue
            if c >= ‘a‘ and c <= ‘z‘:
                c = chr(ord(c) - ord(‘a‘) + ord(‘A‘))
            lst.append(c)
        ret = ‘‘
        for i in range(len(lst)):
            ret += lst[i]
            rem = len(lst) % K - 1
            if rem < 0:
                rem += K
            if i != len(lst) - 1 and i % K == rem:
                ret += ‘-‘
        return ret
            

  

[leetcode]License Key Formatting

原文:https://www.cnblogs.com/lautsie/p/12244785.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!