“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
Flip the string
1. Title Description
Write a function that reverses the input string. The input string is given in the form of the character array char[].
Instead of assigning extra space to another array, you must modify the input array to use O(1) extra space to solve the problem.
You can assume that all the characters in the array are printable characters in the code table.
Example 1:
Input:"h"."e"."l"."l"."o"] output: ["o"."l"."l"."e"."h"]
Copy the code
Example 2:
Input:"H"."a"."n"."n"."a"."h"] output: ["h"."a"."n"."n"."a"."H"]
Copy the code
2. How to solve the problem
-
Built-in Slices in Python
Do this using the slice and step size of a list in Python
-
Double pointer method
Use double Pointers
A header pointer
A tail pointer
Swaps the values of the elements corresponding to the header and tail Pointers
Know that the head pointer is smaller than the tail pointer
Three, code,
-
slice
class Solution: def reverseString(self, s: List[str]) - >None: """ Do not return anything, modify s in-place instead. """ s[:] = s[::-1] Copy the code
-
Double pointer
class Solution: def reverseString(self, s: List[str]) - >None: """ Do not return anything, modify s in-place instead. """ start = 0 end = len(s)-1 while start < end: mid = s[start] s[start] = s[end] s[end] = mid start += 1 end -= 1 Copy the code
Second,
1. Title Description
Given a string, you need to reverse the character order of each word in the string, while still preserving the Spaces and the original order of the words.
Example:
Enter: “Let’s take LeetCode contest” Output: “s ‘tel ekat edoCteeL tsetnoc”
Tip:
In a string, each word is separated by a single space, and there are no extra Spaces in the string.
Second, the topic analysis
-
The Python built-in function method
-
It doesn’t say it’s in the same place
-
So we split the string with a space (split())
-
I have a list of strings
-
Then reverse order the list elements (slice)
-
Finally, convert the list to a string (join())
-
-
Double pointer method
- Iterate over the string
- Stop on whitespace
- And now I have the space subscript (I)
- Subscript j= 0 and then the space subscript I
- I loop over j and I -1 until j < I
- And then the next word
- The end of the
Three, code,
-
Python built-in functions
class Solution: def reverseWords(self, s: str) - >str: s_li = s.split(' ') for i in range(len(s_li)): s_li[i] = s_li[i][::-1] print(s_li[i]) return ' '.join(s_li) Copy the code
-
Double pointer
class Solution: def reverseWords(self, s: str) - >str: sli = list(s) # Convert a string to a list m = 0 x = len(s)-1 for i in range(len(sli)): if sli[i] == ' ': n = i-1 while m < n: mid = s[m] sli[m] = sli[n] sli[n] = mid m += 1 n -= 1 m = i+1 while m < x: # There is no space after the last word in the string mid = s[m] sli[m] = sli[x] sli[x] = mid m += 1 x -= 1 return ' '.join(sli) Copy the code
-
Double pointer II
class Solution: def reverseWords(self, s: str) - >str: sli = list(s) m = 0 sli.append("") for i in range(len(sli)): if sli[i] == ' ': n = i-1 while m < n: mid = s[m] sli[m] = sli[n] sli[n] = mid m += 1 n -= 1 m = i+1 sli[len(sli)-1] = ' ' return ' '.join(sli) Copy the code
Add a space and then delete it