- 实现字符串倒序显示
# 方法1:
return sentence[::-1]
# 方法2:
l = list(sentence)
l.reverse()
l = ''.join(l)
- 让一段话中每个单词的首字母大写

image.png
def isUnique(self, astr):
"""
:type astr: str
:rtype: bool
"""
方法一:
# import re
# for i in astr:
# if len(re.findall(i, astr)) > 1:
# return False
# return True
方法二:
return len(astr)-len(set(astr)) < 1
def CheckPermutation(self, s1, s2):
"""
给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
:type s1: str
:type s2: str
:rtype: bool
"""
方法一:
# import re
# count = 0
# if len(s1) == len(s2):
# for i in set(s1):
# if len(re.findall(i, s1)) == len(re.findall(i, s2)):
# count += 1
# if count == len(set(s1)):
# return True
# return False
方法二:
if len(s1) != len(s2): return False
return sorted(list(s1)) == sorted(list(s2))
网友评论