题解 | #字符串变形#

字符串变形

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

全部评论

相关推荐

牛牛不会牛泪:可以先别急着租房,去青旅,或者订个近点的宾馆待几天。先看看要做的能不能学到东西,然后看文档完不完善,写的好不好,mentor对你咋样,公司氛围啥的。情况不对赶快跑路找下家
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务