题解 | #HJ31 单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
方法1
s = input() new_s = "" for i in s: if 97 <= ord(i.lower()) <= 122: new_s += i else: new_s += " " arr = new_s.split() print(" ".join(arr[::-1]))
方法2
a = input() for i in a: if not 97 <= ord(i.lower()) <= 122: a = a.replace(i, " ") b = a.split() print(*b[::-1])
方法3
s = input().strip() + " " word = '' word_list = [] for c in s: if c.isalpha(): word += c else: word_list.append(word) word = '' print(' '.join(word_list[::-1]))
注意:使用split()而不是split(' '),因为如果两字母间存在多个空格时,空格也会被分割出来,而题目要求只允许出现一个空格。
【牛客站内】华为机试题解 文章被收录于专栏
【牛客站内】 分享个人刷题的思路和解法