【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
随机推荐
- day9 图书设计项目
总路由层url from django.conf.urls import url from django.contrib import admin from app01 import views ur ...
- Shell学习(五)—— awk命令详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报 ...
- c学习 - 第四章:顺序程序设计
4.4 字符数据的输入输出 putchar:函数的作用是想终端输出一个字符 putchar(c) getchar:函数的作用是从输入设备获取一个字符 getchar(c) 4.5 格式输入与输出 pr ...
- Java_zip_多源文件压缩到指定目录下
依赖: <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <depend ...
- 『学了就忘』Linux服务管理 — 77、RPM包安装基于xinetd的服务的管理
目录 1.基于xinetd服务的启动管理 (1)telnet服务安装 (2)telnet服务启动 2.基于xientd服务的自启动管理 现在Linux系统中基于xinetd的服务越来越少了,但Linu ...
- Python绘制折线图
一.Python绘制折线图 1.1.Python绘制折线图对应代码如下图所示 import matplotlib.pyplot as pltimport numpy as np from pylab ...
- noVNC实现浏览器远程访问Windows桌面
一.简介 1.VNC介绍 VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件.VNC 是在基于 UNIX 和 Linux 操作系统的免费 ...
- pf4j及pf4j-spring
什么是PF4J 一个插件框架,用于实现插件的动态加载,支持的插件格式(zip.jar). 核心组件 Plugin:是所有插件类型的基类.每个插件都被加载到一个单独的类加载器中以避免冲突. Plugin ...
- 开源企业平台Odoo 15社区版之项目管理应用模块功能简介
项目管理无论是各类证书的认证,如PMP.软考高级的信息系统项目管理师.中级的系统集成项目管理工程师等,还是企业实践都有着广泛的实际应用中,至今还是处于热门的行业,合格的或优化的项目经理还是偏少,对于I ...
- Table.PromoteHeaders升降标题Table…Headers(Power Query 之 M 语言)
数据源: 任意数据源 目标: 将第一行提升为标题 操作过程: [主页](或[转换])>[将第一行用作标题] M公式: = Table.PromoteHeaders( 表, [PromoteAll ...