【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/reverse-words-in-a-string/description/
题目描述
Given an input string, reverse the string word by word.
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up: For C programmers, try to solve it in-place in O(1) space.
题目大意
翻转字符串里面的单词。同时去掉多余的空格。
解题方法
字符串操作直接放弃 C++ ,一般都选择 Python。建议大家刷题的时候会 Python。
Python 的 split() 函数:
- split()的时候,多个连续空格当成分割符;
- split(’ ')的时候,多个空格都要分割,每个空格分割出来空。
所以可以直接用split()函数,把多个连续空格当成分割符,进行分割。
然后再翻转字符串。
class Solution:
def reverseWords(self, s: str) -> str:
return " ".join(s.split()[::-1])
日期
2018 年 6 月 26 日 —— 早睡早起
2020 年 4 月 10 日 —— 春眠不觉晓
【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)的更多相关文章
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- LeetCode 151. 翻转字符串里的单词(Reverse Words in a String)
151. 翻转字符串里的单词 151. Reverse Words in a String
随机推荐
- linux下面升级 Python版本并修改yum属性信息
最近需要在linux下使用python,故需要升级一下python版本,上网查询了一下相关资料,更新了一下linux下面的python环境,记录如下: linux下面升级 Python版本并修改yum ...
- C++20协程实例:携程化的IOCP服务端/客户端
VC支持协程已经有一段时间了,之前一直想不明白协程的意义在哪里,前几天拉屎的时候突然灵光一闪: 以下是伪代码: task server() { for (;;) { sock_context s = ...
- 计算机网络-4-7-内部网关协议OSPF
内部网关协议OSPF(开放最短路径优先) 出现的原因:为了克服RIP协议的缺点在1989年开发出来,**开放 表明OSPF协议不受任何厂家的限制.最短路径优先是因为使用了最短路径算法SPF**. OS ...
- JavaSE中级篇1 — 核心思想:面向对象 — 更新完毕
1.面向对象编程思想(重点中的重点) 题外话: 其他都还可以是技术,但这里是走自己的路--面向对象编程,即:OOP,养成的思想就是:万物皆对象,懂得把东西抽离出来 这一部分记的理论知识很多,而且需要自 ...
- 振鹏学习Java的第二天!
一.今日收获 1.了解了eclipse的具体使用方法. 2.学习了Java程序设计完全手册的第一章内容,明白了相关知识. 3.通过看哔哩哔哩的java的教程视频了解了Dos命令及java的变量和常量. ...
- day06 视图层
day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...
- day08 文件属性
day08 系统目录 今日内容 一.重要目录 1./usr 2./var 3./proc 二.文件的属性 1.文件属性的介绍 2.文件属性的详述 3.企业案例 /usr 安装第三方软件的目录: 1./ ...
- day02 Linux基础
day02 Linux基础 1.什么是服务器 服务器,也称伺服器,是提供计算服务的设备.由于服务器需要响应服务请求,并进行处理,因 此一般来说服务器应具备承担服务并且保障服务的能力. windows: ...
- 2021广东工业大学十月月赛 F-hnjhd爱序列
题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...
- Java 总纲
Java基础篇 Java资源下载 IntelliJ IDEA为类和方法自动添加注释 为什么JAVA对象需要实现序列化? maven ubantu安装maven Java Maven项目搭建 maven ...