码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]License Key Formatting

时间:2020-01-31 12:22:56      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:mat   att   pytho   form   for   turn   ret   format   pre   

注意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

标签:mat   att   pytho   form   for   turn   ret   format   pre   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!