当前位置:网站首页>Leetcode 557. reverse word III in string
Leetcode 557. reverse word III in string
2022-07-29 06:23:00 【Zhangchuming ZCM】
Power button | 557. Invert the words in the string III
Title screenshot

Method 1 : Use extra space
First set up a new list ans, Store the original string in the list after traversal, decomposition and inversion , Final reuse join() The function converts the list into a new string and returns .
Traversal methods : Go through it from beginning to end , Set two pointers i and start.
Shilling start be equal to i, When there is no space i Add up ;
And then from start To i Start traversing and reverse in ans Fill the list with elements ;
When you encounter a space i Accumulate again , And in ans Add spaces to the list ;
Make start Again, it's equal to i, Then repeat the first step 、 And the second part continues to traverse the elements in reverse order , Until the outer layer traversal is completed . Then convert the list to a string .
This algorithm is equivalent to starting to fill in the front non space elements in reverse order after encountering spaces every time , Finally, the words that meet the requirements of the title are in reverse order , But the position of each word is sequential .
Python join() Method | Novice tutorial
def reverseWords(self, s: str) -> str:
n = len(s)
ans=[]
i = 0
while i < n:
start = i
while i < n and s[i] !=" ":
i += 1
for p in range(start, i):
ans.append(s[start + i - 1 -p])
while i < n and s[i] ==" ":
i += 1
ans.append(" ")
return "".join(ans)Complete test code
class Solution:
def reverseWords(self, s: str) -> str:
n = len(s)
ans=[]
i = 0
while i < n:
start = i
while i < n and s[i] !=" ":
i += 1
for p in range(start, i):
ans.append(s[start + i - 1 -p])
while i < n and s[i] ==" ":
i += 1
ans.append(" ")
return "".join(ans)
class main:
a = Solution()
s = "Let's take LeetCode contest"
print(s)
b=a.reverseWords(s)
print(b)
if __name__ == '__main__':
main()
Method 2 : utilize python Of split() Function traversal slice inversion
Direct use of split() The function cuts the word out of the string and reverses , And then use it join() Function is combined into a new string to return .
Python3 split() Method | Novice tutorial
class Solution(object):
def reverseWords(self, s):
return " ".join(word[::-1] for word in s.split(" "))Method 3 : Two slices
You can also avoid traversal , Directly reverse with two slices .
s.split(" ") Slicing can cut strings into words , and s.split(" ")[::-1] You can reverse the order of words after cutting the string into words .
" ".join() You can turn the word list into a string , and " ".join()[::-1] You can invert the string that the word list is converted to .
therefore " ".join(s.split(" ")[::-1])[::-1] Two reversals of , You can invert the letters of each word in the string , But the order of words remains the same .
class Solution(object):
def reverseWords(self, s):
return " ".join(s.split(" ")[::-1])[::-1]Or you can reverse the string first , Then reverse the order of each word .
class Solution(object):
def reverseWords(self, s):
return " ".join(s[::-1].split(" ")[::-1])边栏推荐
猜你喜欢

Leetcode 14. longest public prefix

Huawei cloud 14 day Hongmeng device development -day3 kernel development

Leetcode 35. search insertion location

ML7 self study notes

抽象封装继承多态

Dynamic planning summary

LeetCode #876.链表的中间结点

LeetCode #3.无重复字符的最长子串

Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图

Power electronics: single inverter design (matlab program +ad schematic diagram)
随机推荐
Abstract encapsulation inheritance polymorphism
scanBasePackages扫包范围配置
IDEA 实用快捷键 新手必看
Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)
动态规划总结
LeetCode #35.搜索插入位置
【软件工程之美 - 专栏笔记】24 | 技术债务:是继续修修补补凑合着用,还是推翻重来?
传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
IDEA安装scala
Leetcode 13. Roman numeral to integer
SimpleFOC+PlatformIO踩坑之路
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
#7110 数字走向2 题解
关于【链式前向星】的自学理解
[beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
Leetcode 7. integer inversion
Multithreading and concurrency
【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
Eight sorts --------- quick sort
计算机大厂面试题