给你一个字符串 s ,请你反转字符串中 单词 的顺序。
单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
示例 1:
输入:s = "
the sky is blue
"
输出:"
blue is sky the
"
class Solution: def reverseWords(s) : # 将字符串拆分为单词,即转换成列表类型 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)
给定一个非空的字符串
s
,检查是否可以通过由它的一个子串重复多次构成。
示例 1:
输入: s = "abab"
输出: true
解释: 可由子串 "ab" 重复两次构成。
def repeatedSubstringPattern(s): n=len(s) if n<=1: return False res="" for i in range(1,n//2+1): if n%i==0: res=s[:i] if res * n//i ==s: return True return False