1、替换空格
class Solution: # s 源字符串 def replaceSpace(self, s): # write code here lens=len(s) new_array=[‘‘]*lens for i in range(len(s)): new_array[i]=s[i] if s[i]==" ": new_array[i]="%20" return "".join(new_array)
2、正则表达式匹配
class Solution: def match(self,s,pattern): if len(s)==0 and len(pattern)==0: return True if len(s)>0 and len(pattern)==0: return False if len(pattern)>1 and pattern[1]=="*": if len(s)>0 and (s[0]==pattern[0] or pattern[0]==‘.‘): return self.match(s,pattern[2:])or self.match(s[1:],pattern) else: return self.match(s,pattern[2:]) if len(s)>0 and (s[0]==pattern[0] or pattern[0]==‘.‘): return self.match(s[1:],pattern[1:]) else: return False
3、表示数值的字符串
class Solution: # s字符串 def isNumeric(self, s): # write code here dumer,hasE=False,False for i in range(len(s)): if s[i] in [‘e‘,‘E‘]: if hasE or i==len(s)-1: return False hasE=True elif s[i]==‘.‘: if dumer or hasE: return False dumer=True elif s[i] in [‘+‘,‘-‘]: if i>0 and s[i-1] not in [‘e‘,‘E‘]: return False else: if s[i]>‘9‘or s[i]<‘0‘: return False return True
4、字符流中第一个不重复的字符
class Solution: # 返回对应char def __init__(self): self.l=[] def FirstAppearingOnce(self): # write code here for i in self.l: if self.l.count(i)==1: return i return ‘#‘ def Insert(self, char): # write code here return self.l.append(char)
原文:https://www.cnblogs.com/pythonbigdata/p/12732748.html