题解 | #字符串变形#

字符串变形

https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @param n int整型 
# @return string字符串
#
class Solution:
    def trans(self , s: str, n: int) -> str:
        # write code here
        # 经过一遍遍历将字符串逆序,且字母大小写改变
        # A 65 a 97
        res = ""
        s_len = len(s)
        all_words = s.split(" ")
        words_len = len(all_words)
        for j in range(0, words_len):
            word = all_words[words_len - j - 1]
            for cur_ch in word:
                cur_ch_num = ord(cur_ch)
                new_ch = ""
                if cur_ch_num >= 65 and cur_ch_num < 65 + 26:
                    new_ch = chr(cur_ch_num + 32)
                    res = res + new_ch
                else:
                    new_ch = chr(cur_ch_num - 32)
                    res = res + new_ch
            if j != words_len -1:
                res = res + " "     
        
        return res

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务