문자열 offer-- 안전을 증명하려면

제목 설명은
각 공간을 대체 할 문자열을 함수를 구현하십시오 "20 %." 예를 들어, 문자열이있을 때 우리는 행복이다. 문자열이 후 우리 % 20Are % 20Happy를 교체 한 후.

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

제목 설명
하는 단계를 '일치'와 '하는 기능 구현 정규 표현식에'를. 모드 문자 '.'어느 한 캐릭터 '나타냄 (0 시간 포함)을 임의의 횟수 일 수있는 문자 앞에 표시된다'. 이 문제에서, 경기는 전체 패턴이 문자열의 모든 문자와 일치합니다. 예를 들어, 문자열 "AAA"모드와 "AA"와 "AB 및 AC A"일치하지만, "aa.a"와 "AB는 *는"일치하지 않는

# -*- 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"및 "-1E-16"값을 나타낸다. 그러나 "12E", "1a3.14",와 "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

제목 설명
문자의 첫 번째 문자 스트림이 한 번만 표시 찾을 수있는 기능을 구현하십시오. 예를 들어, 문자 스트림은 처음 두 문자를 읽을 때, 첫 번째 문자 만 "g"한 번 발생 "이동". 처음 6 개 문자를 읽을 때 처음으로 하나의 문자 "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

** 참고 : ** 빠른 큐의 문제

출시 구 개 원래 기사 · 원의 칭찬 0 · 조회수 756

추천

출처blog.csdn.net/weixin_42707571/article/details/105249331