leetcode-1417-重新格式化字符串

题目描述:

 

 

 

 第一次提交:

class Solution:
    def reformat(self, s: str) -> str:
        digit = []
        alpha = []
        for i in s:
            if i.isdigit():
                digit.append(i)
            elif i.isalpha():
                alpha.append(i)
        if (len(digit) - len(alpha)) not in [1,0,-1]:
                return ""
        ans = ""
        while digit and alpha:
            ans += digit.pop(0)
            ans += alpha.pop(0)
        if digit:
            ans += digit[-1]
        if alpha:
            ans = alpha[-1] + ans
        return ans   //建议用列表 return "".join(ans)

 

posted @ 2020-04-20 20:29  oldby  阅读(163)  评论(0编辑  收藏  举报