「LeetCode每日一题」—— 151. 翻转字符串里的单词
151. 翻转字符串里的单词
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/
难度:中等
题目
给定一个字符串,逐个翻转字符串中的每个单词。
示例 1:
输入: "the sky is blue"
输出: "blue is sky the"
示例 2:
输入: " hello world! "
输出: "world! hello"
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
示例 3:
输入: "a good example"
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
思路
内置函数调用:
使用 split 将字符串按空格分割成字符串数组;
使用 reverse 将字符串数组进行反转;
使用 join 方法将字符串数组拼成一个字符串。


当然你也可以自己写,代码见解决方案。
方案代码
解决方案:
class Solution:
def reverseWords(self, s: str) -> str:
return " ".join(reversed(s.split()))
相关
原创文章,作者:flypython,如若转载,请注明出处:http://flypython.com/algorithm/leetcode/319.html
相关推荐
-
「LeetCode每日一题」——1071. 字符串的最大公因子
1071. 字符串的最大公因子 链接:https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/难度:…
12/03/2020 -
「LeetCode每日一题」——945. 使数组唯一的最小增量
945. 使数组唯一的最小增量 链接:https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique…
22/03/2020 -
「LeetCode每日一题」—— 1095. 山脉数组中查找目标值
1095. 山脉数组中查找目标值 链接:https://leetcode-cn.com/problems/find-in-mountain-array/难度:困难 题目 点击原文链…
30/04/2020 -
「LeetCode每日一题」225. 用队列实现栈
LeetCode每日一题 周五跟大家预告了LeetCode每日一题的活动,今天活动已经开始了。打开leetcode中文版,你可以在题库中看到制定的题目,行动起来吧。 在这里帖下打卡…
01/03/2020 -
「LeetCode每日一题」——994. 腐烂的橘子
994. 腐烂的橘子 链接:https://leetcode-cn.com/problems/rotting-oranges/难度:简单 题目 在给定的网格中,每个单元格可以有以下…
04/03/2020 -
「LeetCode每日一题」—— LOCF.51. 数组中的逆序对
LOCF.51. 数组中的逆序对 链接:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/难度:中等 …
24/04/2020 -
「LeetCode每日一题」——121. 买卖股票的最佳时机
121. 买卖股票的最佳时机 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/难度:简单 题…
09/03/2020 -
「LeetCode每日一题」—— 55. 跳跃游戏
55. 跳跃游戏 链接:https://leetcode-cn.com/problems/jump-game/难度:中等 题目 思路 这题很容易想到从后面开始往前面跳。如果倒数第一…
17/04/2020 -
「LeetCode每日一题」——543. 二叉树的直径
543. 二叉树的直径 链接:https://leetcode-cn.com/problems/diameter-of-binary-tree/难度:简单 题目 给定一棵二叉树,你…
10/03/2020 -
「LeetCode每日一题」—— 22. 括号生成
22. 括号生成 链接:https://leetcode-cn.com/problems/generate-parentheses/难度:中等 题目 数字 n 代表生成括…
09/04/2020
您必须登录才能发表评论。