Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the word problem in flipped string by leecode

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you leecode how to solve the word problem in the flipped string, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Given a string, flip each word in the string one by one.

Example 1: input: "the sky is blue" output: "blue is sky the" example 2:

Input: "hello world!" output: "world! hello" explanation: the input string can contain extra spaces before or after, but inverted characters cannot be included.

Example 3: input: "a good example" output: "example good a" explanation: if there is an extra space between two words, reduce the space between the inverted words to only one.

Description:

No space characters form a word.

The input string can contain extra spaces before or after it, but the inverted characters cannot be included.

If there is an extra space between two words, reduce the space between the inverted words to only one.

Advanced:

Users who choose C language should try to use the in-situ solution of O (1) extra space complexity.

Test points: string-related operations

Train of thought:

Convert to a list of strings, reverse stitching

Double pointers, save spaces to the list when encountered

Class Solution: def reverseWords (self, s: str)-> str: if s is None or len (s)

< 1: return s ''' new_s = s.strip(' ') new_s = new_s.split() new_s.reverse() return ' '.join(new_s) ''' # 双指针 res = [] s = s.strip() i = j = len(s) - 1 res = [] while i >

= 0: while I > = 0 and s [I]! ='': I-= 1 # search for the first space res.append (s [I + 1: J + 1]) # add the word while s [I] = ='': I-= 1 # skip the space j between words = I # j points to the trailing character return''.join (res) # of the next word and returns all the contents of the article "how leecode solves the word problem in flipped strings" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report