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

  

相关文章:

  • 2022-02-10
  • 2021-10-17
  • 2021-06-16
  • 2021-12-24
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-05-29
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2021-08-30
相关资源
相似解决方案