Prove safety offer0507: regularization expression matching; represents a numeric string; character stream does not repeat the first character

Regular expression matching of the
subject 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

Analysis:
Links: https://www.nowcoder.com/questionTerminal/45327ae22b7b413ea21df13ee7d6429c
Source: Cattle-off network

When the second mode is the character " " is:
If the first character string pattern does not match with the first character, then the shift mode after two characters, continues to match. If the first character string with the first character pattern matching, there may be three kinds of matching mode:
1, 2 character pattern after the shift, corresponds to the x
is ignored;
2, move a character string, characters after the mode shift 2 ;
3, move a character string, a mode change, i.e., continues to match the next character, as can a number of match;
when the mode is not the second character "
" is:
1, if the first character mode and the first character matches the character string and a character backward modes, then the remaining matches.
2, if the first character and the first character pattern does not match the phase directly returns false.

# -*- 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 (pattern[0]=='.' or s[0]==pattern[0])):
            return self.match(s[1:],pattern[1:])
        return False

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

Notes; python take a shortcut, regularization expressions do not understand, first posted about the idea
Solution 1:

# -*- coding:utf-8 -*-
class Solution:
    # s字符串
    def isNumeric(self, s):
        # write code here
        try:
            p = float(s)
            return True
        except:
            return False

Solution 2:
Links: https://www.nowcoder.com/questionTerminal/6f8c901d091949a5837e24bb82a731f2
Source: Cattle-off network

Several key points:
1. The basic boundary. string == NULL || * string == ' \ 0'

2. detect whether the sign bit

3. Detection of a valid bit except the sign bit of the valid bit only or decimal numbers.

4. detect whether or E e, and can not be repeated

The decimal point can not be repeated

6. Digital legitimacy can not be other letters such as 'a', etc.

链接:https://www.nowcoder.com/questionTerminal/6f8c901d091949a5837e24bb82a731f2
来源:牛客网

class Solution {
public:
    bool isNumeric(char* str) {
        // 标记符号、小数点、e是否出现过
        bool sign = false, decimal = false, hasE = false;
        for (int i = 0; i < strlen(str); i++) {
            if (str[i] == 'e' || str[i] == 'E') {
                if (i == strlen(str)-1) return false; // e后面一定要接数字
                if (hasE) return false;  // 不能同时存在两个e
                hasE = true;
            } else if (str[i] == '+' || str[i] == '-') {
                // 第二次出现+-符号,则必须紧接在e之后
                if (sign && str[i-1] != 'e' && str[i-1] != 'E') return false;
                // 第一次出现+-符号,且不是在字符串开头,则也必须紧接在e之后
                if (!sign && i > 0 && str[i-1] != 'e' && str[i-1] != 'E') return false;
                sign = true;
            } else if (str[i] == '.') {
              // e后面不能接小数点,小数点不能出现两次
                if (hasE || decimal) return false;
                decimal = true;
            } else if (str[i] < '0' || str[i] > '9') // 不合法字符
                return false;
        }
        return true;
    }
};

The first 3 characters in the stream of unique characters
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.

Notes; did not understand the beginning, read through the code and see ~

# -*- coding:utf-8 -*-
class Solution:
    # 返回对应char
    def __init__(self):
        self.s = ''
        self.dict1 = {}
    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 = self.s + char
        if char in self.dict1:
            self.dict1[char] = self.dict1[char] + 1
        else:
            self.dict1[char] = 1

Guess you like

Origin blog.csdn.net/Leia21/article/details/89954477