class Solution:
def reverseString(self, s: List[str]) -> 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
return
class Solution:
def reverseStr(self, s: str, k: int) -> str:
curr = 0
n = len(s)
while(curr < n):
if n - curr < k:
s = self.reverse(s, curr, n-1)
elif n-curr > 2*k:
s = self.reverse(s, curr, curr+k-1)
else:
s = self.reverse(s, curr, curr+k-1)
curr += 2*k
return s
def reverse(self, s, left, right):
s = list(s)
while(left < right):
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
return "".join(s)
str1 = input()
def replace(s):
s = list(s)
output = ""
for i in range(len(s)):
if s[i] >= 'a' and s[i] <= 'z':
output += s[i]
else:
output += 'number'
return output
ans = replace(str1)
print(ans)
class Solution:
def reverseWords(self, s: str) -> str:
# 从后往前的双指针
n = len(s)
i = n-1
j = n-1
res = []
while i >= 0:
while i >=0 and s[i] == ' ': # 跳过开头的空格
i -= 1
j = i
while i >=0 and s[i] != ' ':
i -= 1 # 找空格
res.append(s[i+1:j+1])
while i >=0 and s[i] == ' ': # 跳过结尾的空格
i -= 1
j = i
print(res)
return " ".join(res)
调库做法:
class Solution:
def reverseWords(self, s: str) -> str:
# 删除前后空白
s = s.strip()
# 反转整个字符串
s = s[::-1]
# 将字符串拆分为单词,并反转每个单词
s = ' '.join(word[::-1] for word in s.split())
return s
class Solution:
def reverseWords(self, s: str) -> str:
# 将字符串拆分为单词,即转换成列表类型
words = s.split()
# 反转单词
left, right = 0, len(words) - 1
while left < right:
words[left], words[right] = words[right], words[left]
left += 1
right -= 1
# 将列表转换成字符串
return " ".join(words)
卡码网用的是ACM模式,没怎么写过,很生疏,不会写输入输出,不过微信公众号上有一个模板
import sys
while True:
s = input().split() # 一行一行读取
a, b = int(s[0]), int(s[1])
if not a and not b: # 遇到 0, 0 则中断
break
print(a + b)
其实一直不太知道python做反转字符串的知识点是啥。。
import sys
k = int(input())
s = input()
# 1.在本地进行修改。也许吧
# 整体翻转,再局部翻转
s = "".join(reversed(s))
s = s[:k][::-1] + s[k:][::-1]
print(s)
# 2.不在本地进行修改
output = ""
n = len(s)
output = s[n-k:n] + s[:n-k]
print(output)