344.反转字符串
初步想法
用双指针从字符串中间开始向两头走,凉凉交换
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
if len(s) % 2 == 0:
left = len(s)//2 - 1
right = len(s)//2
if len(s) % 2 == 1:
left = len(s)//2 - 1
right = len(s)//2 + 1
while left >= 0 and right < len(s):
temp = s[left]
s[left] = s[right]
s[right] = temp
left -= 1
right += 1
题解
https://programmercarl.com/0344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.html
一样的,只不过是从两头开始
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left, right = 0, len(s) - 1
# 该方法已经不需要判断奇偶数,经测试后时间空间复杂度比用 for i in range(len(s)//2)更低
# 因为while每次循环需要进行条件判断,而range函数不需要,直接生成数字,因此时间复杂度更低。推荐使用range
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
541. 反转字符串II
初步想法
想法跟上一题一样,但是一直有bug
题解
for i in range(0,len(s)):
if i + k <= len(s):
reverse(s,i,i+k)
continue
reverse(s,i,len(s))
code:
class Solution:
def reverseStr(self, s: str, k: int) -> str:
"""
1. 使用range(start, end, step)来确定需要调换的初始位置
2. 对于字符串s = 'abc',如果使用s[0:999] ===> 'abc'。字符串末尾如果超过最大长度,则会返回至字符串最后一个值,这个特性可以避免一些边界条件的处理。
3. 用切片整体替换,而不是一个个替换.
"""
def reverse_substring(text):
left, right = 0, len(text) - 1
while left < right:
text[left], text[right] = text[right], text[left]
left += 1
right -= 1
return text
res = list(s)
for cur in range(0, len(s), 2 * k):
res[cur: cur + k] = reverse_substring(res[cur: cur + k])
return ''.join(res)
另外要注意自己定义的方程是放在reverseStr里面的,不是外面的
卡码网:54.替换数字
初步想法
感觉是不难,但是要怎么判断这个字符是不是数字?
题解
https://programmercarl.com/kama54.%E6%9B%BF%E6%8D%A2%E6%95%B0%E5%AD%97.html#%E6%80%9D%E8%B7%AF
双指针解法,很巧妙,
class Solution:
def replaceNumber(self,s:str) -> str:
result = list(s)
number = ['n','u','m','b','e','r']
count = 0
for i in range(len(result)):
if result[i] >= '0' and result[i] <= '9':
count += 1
result = result + [0]*(count *6-count)
left = len(s) - 1
right = len(result) - 1
while left >= 0:
if result[left] >= '0' and result[left] <= '9':
result[right-6+1:right+1] = number
right = right - 6
left -= 1
else:
result[right] = result[left]
right -= 1
left -= 1
return ''.join(result)
sol = Solution()
s = input()
result = sol.replaceNumber(s)
print(result)
151.翻转字符串里的单词
初步想法
双指针从后开始,一个找非空字符的结尾一个找开头,一个词一个词找,超时了…
题解
https://programmercarl.com/0151.%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E7%9A%84%E5%8D%95%E8%AF%8D.html
删掉多余空格 (第一反应是strip哈哈哈哈)
先将整体字符串进行反转,再针对每个单词进行翻转
使用快慢指针删除多余空格
slow = 0
for fast in range(0,len(s)):
if s[fast] != ' ':
if slow != 0:
s[slow] = ' '
slow += 1
while fast < len(s) and s[fast]!=' ':
s[slow] = s[fast]
slow += 1
fast += 1
code: 写了好久啊啊啊啊啊
class Solution:
def reverseWords(self, s: str) -> str:
def reverse(s:str, left:int,right :int) -> str:
while left < right:
temp = s[left]
s[left] = s[right]
s[right] = temp
left += 1
right -= 1
slow = 0
s = list(s)
fast = 0
while fast < len(s):
if s[fast] != ' ':
if slow != 0:
s[slow] = ' '
slow += 1
while fast < len(s) and s[fast]!=' ':
s[slow] = s[fast]
slow += 1
fast +=1
else:
fast += 1
s = s[0:slow]
left = 0
right = len(s)-1
reverse(s,left,right) #对整体进行翻转
left = 0
right = 0
while right <= len(s):
if right == len(s) or s[right] == ' ':
reverse(s,left,right-1)
left = right + 1
right += 1
else:
right += 1
return ''.join(s)
卡码网:55.右旋转字符串
初步想法
class Solution:
def reversek(self, k:int, s:str) -> str:
return s[-k:] + s[0:len(s)-k]
sol = Solution()
k = int(input())
s = input()
print(sol.reversek(k,s))
题解
https://programmercarl.com/kama55.%E5%8F%B3%E6%97%8B%E5%AD%97%E7%AC%A6%E4%B8%B2.html#%E6%80%9D%E8%B7%AF
不用额外空间:
整体翻转,再将前K个翻转,剩下的再翻转