当前位置:网站首页>leetcode151 翻转字符串里的单词
leetcode151 翻转字符串里的单词
2022-06-12 17:51:00 【zhuxiaohai68】
给你一个字符串 s ,逐个翻转字符串中的所有 单词 。
单词 是由非空格字符组成的字符串。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”
解释:如果两个单词间有多余的空格,将翻转后单词间的空格减少到只含一个。
示例 4:
输入:s = " Bob Loves Alice "
输出:“Alice Loves Bob”
示例 5:
输入:s = “Alice does not even like bob”
输出:“bob like even not does Alice”
python 的字符串无法原地修改,因此只能耗费O(n)的额外空间创建列表
时间复杂度O(n)
class Solution:
def reverseWords(self, s: str) -> str:
# 去掉字符串开头结尾和中间的多余空白字符
s_list = self.trim(s)
# 翻转字符串
self.reverse(s_list, 0, len(s_list)-1)
# 翻转每个单词
self.reverse_words(s_list)
return ''.join(s_list)
def trim(self, s):
left = 0
right = len(s) - 1
# 去掉字符串开头的空白字符
while s[left] == ' ':
left += 1
# 去掉字符串末尾的空白字符
while s[right] == ' ':
right -= 1
# 将字符串间多余的空白字符去除
output = []
while left <= right:
if s[left] == ' ' and output[-1] == ' ':
left += 1
else:
output.append(s[left])
left += 1
return output
def reverse(self, s, l, r):
while l < r:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1
def reverse_words(self, s):
left = 0
right = 0
while right < len(s):
# 循环至单词的末尾
while (right < len(s)) and (s[right] != ' '):
right += 1
# 翻转单词
self.reverse(s, left, right-1)
# 更新start,去找下一个单词
left = right + 1
right = right + 1
边栏推荐
- Is it cost-effective to apply for Huagui sweet home term life insurance? What are the advantages of this product?
- vant3+ts DropdownMenu 下拉菜单,数据多能滚动加载
- Arm64 stack backtracking
- Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
- Tensorflow prompts typeerror: unsupported operand type (s) for *: 'float' and 'nonetype‘
- TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
- 利用小程序快速生成App,只需七步
- Getting started with grpc swift
- EASYCODE template
- Flink 维表异步查询的实现以及问题排查
猜你喜欢

内核中断整体流程图

极限编程--根源分析实践

Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default

Learn the mitmproxy packet capturing tool from scratch

DRM 驱动 mmap 详解:(一)预备知识

JDBC several pits

Hangzhou AI developer meetup registration opens!

用好IDE,研发效能提速100%
![[csp]202012-2 optimal threshold for period end forecast](/img/40/9b59bd692bcfe05d16614cc6d55d1f.png)
[csp]202012-2 optimal threshold for period end forecast

Make good use of IDE, speed up R & D efficiency by 100%
随机推荐
An easy-to-use IDE for small programs
Graphical data model for system design
Advanced mountain -asp Net core router basic use demo 0.1
利用小程序快速生成App,只需七步
《用户体验要素:以用户为中心的产品设计》笔记
Is it cost-effective to apply for Huagui sweet home term life insurance? What are the advantages of this product?
Is Huishang futures company reliable in opening accounts and safe in trading?
Deep interest evolution network for click through rate prediction
The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time zone. ......
Detailed description of SQL cursor and example of internal recycling
406. reconstruct the queue based on height
Lambda - 1
Use the official go Library of mongodb to operate the original mongodb
vant3 +ts 封装简易step进步器组件
系统设计之图状数据模型
Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
极限编程--根源分析实践
Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
JDBC several pits
三代DRI的变化