# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @param n int整型 # @return string字符串 # """ 关键点: ord 的使用 判断字符的ascii 码 大小写字母 字符串数组的逆序: s=s[::-1] " ".join(list) 表示用空格将list 拼接成字符串 """ class Solution: def trans(self , s: str, n: int) -> str: if n==0: return s line=[] for ch in s: if ch.islower(): line.append(ch.upper()) #转为大写字符 elif ch.isupper(): line.append(ch.lower()) else: line.append(" ") # 空格 res="".join(line).split(" ") return " ".join(res[::-1]) def trans1(self , s: str, n: int) -> str: # write code here line="" for ch in s: ret_ch=self.func(ch) line+=str(ret_ch) #分割 st_list=list(line.split(" ")) st_list=st_list[::-1] res=list() return " ".join(st_list) # 用空格隔开 def func(self,ch): if ord("A")<=ord(ch)<=ord("Z"): return ch.lower() if ord("a")<= ord(ch)<=ord("z"): return ch.upper() else: return ch