当前位置:网站首页>Leetcode151 flipping words in strings
Leetcode151 flipping words in strings
2022-06-12 17:59:00 【zhuxiaohai68】
Give you a string s , Flip all in the string one by one word .
word Is a string of non whitespace characters .s Use at least one space in the string word Separate .
Please return to a flip s A string in word order and connected by a single space .
explain :
Input string s It can be in the front 、 Contain extra spaces after or between words .
After flipping, the words should be separated by only one space .
The flipped string should not contain additional spaces .
Example 1:
Input :s = “the sky is blue”
Output :“blue is sky the”
Example 2:
Input :s = " hello world "
Output :“world hello”
explain : The input string can contain extra spaces before or after , However, the flipped characters cannot include .
Example 3:
Input :s = “a good example”
Output :“example good a”
explain : If there are extra spaces between two words , Reduce the space between words after flipping to only contain one .
Example 4:
Input :s = " Bob Loves Alice "
Output :“Alice Loves Bob”
Example 5:
Input :s = “Alice does not even like bob”
Output :“bob like even not does Alice”
python The string of cannot be modified in place , So it can only cost O(n) Extra space to create a list
Time complexity O(n)
class Solution:
def reverseWords(self, s: str) -> str:
# Remove the extra white space characters at the beginning, end and middle of the string
s_list = self.trim(s)
# Flip strings
self.reverse(s_list, 0, len(s_list)-1)
# Flip each word
self.reverse_words(s_list)
return ''.join(s_list)
def trim(self, s):
left = 0
right = len(s) - 1
# Remove the white space character at the beginning of the string
while s[left] == ' ':
left += 1
# Remove the white space character at the end of the string
while s[right] == ' ':
right -= 1
# Remove extra white space characters between strings
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):
# Loop to the end of the word
while (right < len(s)) and (s[right] != ' '):
right += 1
# Flip Words
self.reverse(s, left, right-1)
# to update start, Find the next word
left = right + 1
right = right + 1
边栏推荐
- vant3+ts DropdownMenu 下拉菜单,数据多能滚动加载
- Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
- Global lock, table lock, row lock
- idea 常用快捷键
- Cesium parabolic equation
- Is Huishang futures company reliable in opening accounts and safe in trading?
- Soringboot下RestTemplateConfig 配置打印请求响应日志
- 利用小程序快速生成App,只需七步
- Sqlserver common statements and functions
- leetcode491 递增子序列
猜你喜欢

LCD parameter interpretation and calculation

Arm64 stack backtracking

消息队列实战之队列优先级

Extreme Programming -- Practice of root cause analysis

TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘

JDBC quick start tutorial

Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data

有源差分晶振原理圖以及LV-PECL、LVDS、HCSL區別

App中快速复用微信登录授权的一种方法

vant3 +ts 封装简易step进步器组件
随机推荐
Project training of Software College of Shandong University - Innovation Training - network attack and defense shooting range experimental platform of Software College of Shandong University (XXV) - p
An easy-to-use IDE for small programs
Error record: illegalstateexception: optional int parameter 'XXXX' is
Arm64棧回溯
vant3+ts DropdownMenu 下拉菜单,数据多能滚动加载
Global lock, table lock, row lock
Message queuing MySQL tables that store message data
有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别
Recognize function originality
認識函數原創
TensorFlow从网络读取数据
认识函数原创
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
SQL游标(cursor)详细说明及内部循环使用示例
NixOS 22.05安装过程记录
Two ways of tensorflow2 training data sets
山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(二十五)-项目个人总结
MySQL learning notes
Extreme Programming -- Practice of root cause analysis
Unprecedented analysis of Milvus source code architecture