题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(' ', '%20')
题目描述
请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配
# -*- coding:utf-8 -*-
class Solution:
# s, pattern都是字符串
def match(self, s, pattern):
# write code here
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[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
题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。
class Solution:
# s字符串
def isNumeric(self, s):
# write code here
s = list(s)
count = 0
for i in range(len(s)):
if s[i] in ['+', '-']:
if i and s[i-1] not in ['e', 'E']:
return False
elif s[i] in ['e', 'E']:
if i==len(s)-1 or '.' in s[i+1:]:
return False
elif s[i] == '.':
count += 1
if i==0 or i==len(s)-1 or 'e' in s[:i] or 'E' in s[:i]:
return False
elif s[i]<'0' or s[i]>'9':
return False
return True
题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
# -*- coding:utf-8 -*-
class Solution:
# 返回对应char
def __init__(self):
self.dict1 = {}
self.s = ''
def FirstAppearingOnce(self):
# write code here
for i in self.s:
if self.dict1[i] == 1:
return i
return '#'
def Insert(self, char):
# write code here
self.s += char
if char not in self.dict1:
self.dict1[char] = 1
else:
self.dict1[char] += 1
**注:**本题用队列更快