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

What are the methods of string inversion in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you what are the relevant knowledge points about the methods of string inversion in Python, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Preface

One problem is to implement a function that reverses a string, as follows:

Write a function that reverses the input string. The input string is given as an array of characters char [].

Don't allocate extra space to another array, you must modify the input array in place and use the extra space of O (1) to solve this problem.

We can assume that all characters in the array are printable characters in the ASCII code table.

Example 1:

Enter: ["h", "e", "l", "l", "o"]

Output: ["o", "l", "l", "e", "h"]

Example 2:

Enter: ["H", "a", "n", "n", "a", "h"]

Output: ["h", "a", "n", "n", "a", "H"]

Do it yourself, there are probably the following ways

Method 1

If you do not consider the "in-situ modification of the input array" in the title, you can use an intermediate list to achieve it.

Traversing forward from the end of the list, you can define the index in two ways:

Def reverseString_1 (self, s): "" idea: with the help of an intermediate variable temp, then extract from the tail of s, append the element to temp: param s::return: "temp = [] for i in range (len (s)): temp.append (Slen (s)-1Mui]) return tempdef reverseString_2 (self, s):"idea: same as method 4 But another implementation (like the reverse loop used in method 2 in question 8): param s::return: "" temp = [] for i in range (len (s)-1,-1,-1): # range represents the index range, and len (s)-1 represents the first element of the traversal (that is, starting with the last element) # the first-1 represents the last position, but since the right side of the traversal range is not closed, what is actually traversed here is the first element; # the second-1 represents the step size, each time-1, that is, traversing temp.append (s [I]) return temp method 2 from back to front

Think about it:

Inverted strings can actually be converted into "head-to-tail element string swaps", such as first-and last-bit swaps, second-to-last bit swaps, third-bit and penultimate bit swaps.

[1,2,3,4,5,6]

[6,5,4,3,2,1]

In this case, you can define two pointers, one traversing back from the first position and one traversing from the end to the front.

Then the elements in two positions are exchanged for each traversal, all the way to the intermediate element.

Def reverseString (self, s): "": type s: List [str]: rtype: None Do not return anything, modify s in-place instead. "I = 0 # first pointer, traversing j = len (s)-1 # second pointer from the beginning, traversing while j > I: # if j > I, keep looping. Until the two pointers meet s [I], s [j] = s [j], s [I] # exchange the elements of two positions I + = 1J-= 1return s method 3

This method is rather winding, and when submitting, it fails due to a timeout (the array given by the system is too long). However, several arrays are given during the test, and the output can be reversed normally.

The idea is as follows: since the output is in reverse order, you can swap the first and second elements, and then the second and third … Until the first element is moved to the last position.

But it's not over yet, it's just moving the first element to the end, you need to repeat the above steps, and then move the original second element (now the first element) to the penultimate position (note: the last element at this time cannot be moved)

Follow the above steps, move all the elements, and you are done.

Def reverseString (self, s): "" ideas: type s: List [str]: rtype: None Do not return anything, modify s in-place instead. "n = 0while n < len (s): I = 0for j in range (1, len (s)-n): s [I], s [j] = s [j], s [I] I + = 1n + = 1return s method 4

Use slicing to achieve reverse output:

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange QQ group: 857662006 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Def reverseString (self, s): "" idea: slice s [::-1] means to reverse the element s [:] in the array s [:] = s [::-1] means to invert the original array and assign values to each corresponding position in s [::-1] means to reverse s and assign s to the new object s Not consistent with the original revision of the title: param s::return: "" s [:] = s [::-1] return s method 5

Using the reverse () method, it sorts the original list in reverse (note: reverse is only valid for list)

Def reverseString_6 (self, s): ": param s::return:" s.reverse () return s

However, the source code of the reverse () method will find that it is actually a utilized slice, as follows:

These are all the contents of the article "what are the ways to achieve string inversion in Python?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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

Development

Wechat

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

12
Report