Leetcode brushing record (9): 557 words in reversed string III

Brush questions website: Leetcode

Difficulty: easy

Language: Python

Plan : From easy -> to medium -> to hard.

1. 557 reverse string III

1.1 Subject:

Given a string, you need to reverse the character order of each word in the string, while still preserving the initial order of spaces and words.

  • Example
输入:"Let's take LeetCode contest"
输出:"s'teL ekat edoCteeL tsetnoc"

1.2 Thinking and Analysis:

This problem is to input a string and output the reverse of each word in the string. It seems simple, but it actually takes a little brainstorming. Initially, I wanted to divide the string into several substrings in units of words, and then use the double pointer method to exchange them left and right one by one, and finally recombine them, but I found that it was too troublesome, and some variable transformations in the middle were very cumbersome, It takes a long time to get the desired result. (But after doing it, I found that my idea is a bit similar to the official solution, why didn't I write the code!!!)

Then, I thought that slices in python can reverse strings, before I need to understand what is slice? Here's some stuff I've put together:

  • 1) In python, slicing generally means "cutting" a string into several parts, usually using symbols to perform the slicing operation, which is used to define the slicing and step size.
  • 2) A complete slice expression has two components, which are used to split the three parameters (start_idex : end_index : step).
  • 3) Slice classification: forward cut from left to right (ie ), reverse cutstep=1 from right to left (ie ).step=-1
  • 4) When there is only one , default step=1. For example, s[:i]means from the 0th element to the i-1th element, excluding the ith one. s[2:10:2]Indicates outputting all elements from the 2nd to the 9th with a step size of 2
  • 5) Examples
    • Input string s="hello world!"and print(s[::-1]), output !dlrow olleh.
    • Input string s="hello world!"and print(s[2:10:2]), output lowr.
    • Input string s="hello world!"and print(s[:5:-1]), output !dlrow.

Then, the characters after the slice are divided, and the split()function is used to divide the characters by default.

  • example
a = "hello world!"
print(a.split())

output

['hello','world!']

Therefore, the complete code for this question is as follows

class Solution:
    def reverseWords(self, s: str) -> str:
        s = s[::-1]
        s_list = s.split()
        ss = str()
        for i in range (1, len(s_list)+1):
        	if ss:
        		ss +=" "+s_list[-i]
        	else:
        		ss = s_list[-i]
        return ss

The idea of ​​​​this algorithm is to first flip the entire original string, then split the result (separated by spaces), and then reassemble it from right to left to form a new string output. Therefore, the execution result is as follows.
insert image description here

1.3 Summary

This question focuses on understanding the difference between strings and arrays. It cannot be solved with a simple double pointer, but flips each word.

Guess you like

Origin blog.csdn.net/A33280000f/article/details/121224097