标准双指针(对撞指针),reverse操作
流氓写法:s[:] = s[::-1]
class Solution(object):
def reverseString(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
left = 0
right = len(s)-1
while left<=right:
s[left],s[right] = s[right],s[left]
left+=1
right-=1
class Solution(object):
def reverseString(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
s[:] = s[::-1]
557.反转字符串中的单词Ⅲ
1、新建一个字符串,遍历,遇到空格就把之前的单词逆序存入新字符串。这里可以用栈来实现。遇到空格就出栈,直接实现逆序。
2、不新建数组,原地操作,遇到空格就reverse前面的单词,但是种方法对于string为不可变类型的语言不可以,如:python、Java。
3、利用python的spilt加切片逆序加字符串join操作合并实现。
用空格拼接器字符串str = ' '.join(list) 直接拼接起字符串,没有分隔符str = ''.join(list)
spilt(' ')操作返回的就是分割后的字符串列表
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
return " ".join(word[::-1] for word in s.split(" "))
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
return (' '.join(s.split(' ')[::-1]))[::-1]
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
return ' '.join(s[::-1].split(' ')[::-1])
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
i = j = x = 0
l = len(s)
ans = [0]*l
def swap(nums):
l =0
r=len(nums)-1
while l



