def reverse_words(sentence): words = [] current_word = [] for char in sentence: if char == ' ': if current_word: words.append(''.join(current_word)) current_word = [] else: current_word.append(char) if current_word: words.append(''.join(current_word)) reversed_sentence = '' for i in range(len(words) - 1, -1, -1): reversed_sentence += words[i] if i != 0: reversed_sentence += ' ' return reversed_sentence
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务