当前位置:网站首页>LeetCode中等题之颠倒字符串中的单词
LeetCode中等题之颠倒字符串中的单词
2022-08-10 12:05:00 【·星辰大海】
题目
给你一个字符串 s ,颠倒字符串中 单词 的顺序。
单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
示例 1:
输入:s = “the sky is blue”
输出:“blue is sky the”
示例 2:
输入:s = " hello world "
输出:“world hello”
解释:颠倒后的字符串中不能存在前导空格和尾随空格。
示例 3:
输入:s = “a good example”
输出:“example good a”
解释:如果两个单词间有多余的空格,颠倒后的字符串需要将单词间的空格减少到仅有一个。
提示:
1 <= s.length <= 10^4
s 包含英文大小写字母、数字和空格 ’ ’
s 中 至少存在一个 单词
进阶:如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 O(1) 额外空间复杂度的 原地 解法。
来源:力扣(LeetCode)
解题思路
可以直接利用python的过滤器和字符切片以及join完成题目要求。
class Solution:
def reverseWords(self, s: str) -> str:
s=list(filter(lambda x:x!='',s.split(' ')))
s.reverse()
return ' '.join(s)

题目的进阶要求是需要原地进行操作,我们可以模拟刚才的函数,先对字符串进行去除多余的空格,然后在把整个字符串进行反转,最后遍历整个反转的字符串,针对单个单词再进行逐个反转。
class Solution:
def reverseWords(self, s: str) -> str:
s=s.strip()
i,flag=0,1
while i<len(s):
while not flag and s[i]==' ':
s=s[0:i]+s[i+1:]
flag=1
if flag and s[i]==' ':
flag=0
i+=1
n=len(s)
def swap(a,b,s):
temp=s[a]
s=s[0:a]+s[b]+s[a+1:]
s=s[0:b]+temp+s[b+1:]
return s
for i in range(n//2):
s=swap(i,n-1-i,s)
temp,count='',0
s+=' '
for i in s:
if i!=' ':
temp=i+temp
else:
s=s[0:count-len(temp)]+temp+s[count:]
temp=''
count+=1
return s[:-1]

边栏推荐
- The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
- Servlet---Solve the problem of Chinese garbled characters in post requests
- [List merge] Combine multiple lists into one list
- dedecms supports one-click import of Word content
- Chapter9 : De Novo Molecular Design with Chemical Language Models
- LeetCode 21. Merge two ordered linked lists
- 爱可可AI前沿推介(8.10)
- 【list合并】多个list合并为一个list
- What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch
- 【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
猜你喜欢

10 款更先进的开源命令行工具

MySQL面试题——MySQL常见查询

实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!

16. Getting Started with Pytorch Lightning

mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull

「企业架构」应用架构概述

three.js blur glass effect

The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp

Crypto Gaming: The Future of Gaming

如何让别人看不懂你的 JS 代码?把你当大佬!
随机推荐
Threshold-based filtering buffer management scheme in a shared buffer packet switch core part of the paper
托米的咒语
百度用户产品流批一体的实时数仓实践
实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
LT8911EXB MIPI CSI/DSI转EDP信号转换
Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
LeetCode 445. Adding Two Numbers II
【集合】HashSet和ArrayList的查找Contains()时间复杂度
Hackbar 使用教程
LeetCode 237. Delete a node in a linked list
制品库是什么?
如何让别人看不懂你的 JS 代码?把你当大佬!
关于flask中static_folder 和 static_url_path参数理解
tommy's spell
CodeForces - 628D (digital dp)
基于PLECS的离网(孤岛)并联逆变器的Droop Control下垂控制仿真
LeetCode 369. Plus One Linked List
郭晶晶家的象棋私教,好家伙是个机器人
A detailed explanation of implementation api embed
An enhanced dynamic packet buffer management. The core part of the paper