翻转字符串中的单词
strip()去除字符串首尾的指定字符。
split()按指定字符进行字符串切割,返回一个以字符串为元素的列表。
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
str = s.strip()
str = str.split() # 变成一个元素为字符串类型的列表
str = str[::-1]
return ' '.join(str)
右旋字符串
class Solution:
def rightTrans(self,s,k):
s = s[len(s)-k:]+s[:len(s)-k]
return s
实现strStr()
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
num1 = len(needle)
num2 = len(haystack)
if num1>num2:
return -1
for i in range(num2-num1+1):
if haystack[i:i+num1] == needle:
return i
return -1
重复的子字符串
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
if n <= 1:
return False
ss = s[1:] + s[:-1]
print(ss.find(s))
return ss.find(s) != -1