To prove safety offer-- string

Title Description
Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.

# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        return s.replace(' ', '%20')

Title Description
Implement comprises a function to match '' and ' ' in a regular expression. Mode character '.' Represents any one character ' ' is represented in front of the character may be any number of times (including 0 time). In this problem, the match is a whole pattern matches all characters of the string. For example, the string "aaa" mode and "aa" and "ab & AC A" match, but the "aa.a" and "ab * a" does not match

# -*- 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

Title Description
Please implement a function used to determine whether a numerical value string (including integer and fractional). For example, the string "+100", "5e2",and "-1E-16" shows the value. However, "12e", "1a3.14",and "12e + 4.3" not.

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

Title Description
Please implement a function to find the first character stream of characters appear only once. For example, when the character stream reads only the first two characters "go", the first character only occurs once a "g". When reading out the first six characters "google" from the character stream, first appears only one character is "l".
Output Description:
If the current character stream does not appear there is a character, returns the # character.

# -*- 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

** Note: ** The problem with the queue faster

Released nine original articles · won praise 0 · Views 756

Guess you like

Origin blog.csdn.net/weixin_42707571/article/details/105249331
Recommended