leetcode 65. 有效数字 66. 寻找两个正序数组的中位数

65. 有效数字

难度困难295

有效数字(按顺序)可以分成以下几个部分:

  1. 一个 小数 或者 整数
  2. (可选)一个 'e''E' ,后面跟着一个 整数

小数(按顺序)可以分成以下几个部分:

  1. (可选)一个符号字符('+''-'
  2. 下述格式之一:
  1. 至少一位数字,后面跟着一个点 '.'
  2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
  3. 一个点 '.' ,后面跟着至少一位数字

整数(按顺序)可以分成以下几个部分:

  1. (可选)一个符号字符('+''-'
  2. 至少一位数字

部分有效数字列举如下:

  • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

部分无效数字列举如下:

  • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true

示例 1:

输入:s = "0"
输出:true

示例 2:

输入:s = "e"
输出:false

示例 3:

输入:s = "."
输出:false

示例 4:

输入:s = ".1"
输出:true

提示:

  • 1 <= s.length <= 20
  • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.'
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time    : 2022/1/14 14:52 
# @Author  : mtl
# @Desc    : ***
# @File    : 65.py
# @Software: PyCharm
import traceback
from typing import List
from enum import Enum

class Solution:
    def isNumber(self, s: str) -> bool:
        if s == ".":
            return False
        def find(patten: str, s: str):
            plen, slen = len(patten), len(s)
            if slen == 0 or s == ".":
                return False
            i, j = 0, 0
            # jump = True
            arr_num = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
            while i < plen:
                pstr = patten[i]
                if patten[i] == "(":
                    i2 = i + 1
                    while i2 < plen:
                        if patten[i2] == ")":
                            pstr = patten[i+1:i2].split("|")
                            break
                        i2+=1
                    i = i2
                elif patten[i] == "\\":
                    i += 1
                    pstr = patten[i]
                    if pstr == "d":
                        pstr = arr_num
                elif patten[i] not in arr_num:
                    pstr = patten[i]
                i += 1
                flag = False
                while j < slen:
                    flag = False
                    if isinstance(pstr, List):
                        if s[j] in pstr:
                            flag = True
                            j += 1
                    if isinstance(pstr, str):
                        if s[j] == pstr:
                            flag = True
                            j += 1
                    if patten[i] == "?":
                        flag = True
                        i += 1
                    elif patten[i] != "*" and not flag:
                        return False
                    if i >= plen or patten[i] != "*":
                        break
                    if not flag:
                        i += 1
                        break
                if j >= slen:
                    if str(patten[i]) in ["*", "?"]:
                        i += 1
                    elif not flag:
                        break
            if i != plen or j != slen:
                return False
            return True
        s = s.upper()
        if "e" in s or "E" in s:
            sarr = s.split("E")
            if len(sarr) > 2:
                return False
            if "." in sarr[0]:
                f1 = find("(+|-)?\d\d*.?\d*", sarr[0]) or find("(+|-)?\d*.?\d\d*", sarr[0])
            else:
                f1 = find("(+|-)?\d\d*", sarr[0])
            f2 = find("(+|-)?\d\d*", sarr[1])
            return f1 and f2
        else:
            if "." in s:
                return find("(+|-)?\d\d*.?\d*", s) or find("(+|-)?\d*.?\d\d*", s)
            return find("(+|-)?\d\d*", s)

    def isNumber(self, s: str):
        arr_num = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

        def getTask(patten: str):
            i, plen = 0, len(patten)
            while i < plen:
                if patten[i] == "(":
                    i2 = i + 1
                    while i2 < plen:
                        if patten[i2] == ")":
                            return i2 + 1, patten[i + 1:i2].split("|")
                        i2 += 1
                if patten[i] == "\\":
                    i += 1
                    pstr = patten[i]
                    if pstr == "d":
                        return i + 1, arr_num
                return i + 1, patten[i]

        def search(s: str, patten):
            if s == "" or s == ".":
                return False
            l, flag = "*", True
            while flag and len(patten) > 0:
                i, p = getTask(patten)
                l = patten[i] if len(patten) > i else ""
                flag = False
                if len(s) > 0:
                    if isinstance(p, str):
                        if s[0] == p:
                            flag = True
                    elif isinstance(p, List):
                        if s[0] in p:
                            flag = True
                    else:
                        flag = False
                    if flag:
                        s = s[1:]
                if i >= len(patten) and len(s) > 0:
                    flag = False
                    break
                if l == "?":
                    flag = True
                    patten = patten[i + 1:]
                elif l != "*":
                    patten = patten[i:]
                elif l == "*" and not flag:
                    flag = True
                    patten = patten[i + 1:]
                if flag and len(s) > 0 and patten == "":
                    flag = False
                    break
            return flag
        pattens = ["(+|-)?\d\d*.", "(+|-)?\d\d*.\d\d*", "(+|-)?.\d\d*", "(+|-)?\d\d*"]
        flag = False
        sarr = s.upper().split("E")
        if len(sarr) > 2:
            return False
        for patten in pattens:
            s2 = sarr[0]
            flag = search(s2, patten)
            if flag:
                break
        if len(sarr) > 1 and flag:
            flag = search(sarr[1], "(+|-)?\d\d*")
        return flag

    def isNumber(self, s: str):
        if s == "":
            return False
        i, slen = 0, len(s)
        add_flag, num_flag, point_flag, has_point, e_flag, flag = False, False, False, False, False, True
        num_arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."]
        symbol_arr = ["+", "-", "e", "E"]
        while i < slen:
            if s[i] in symbol_arr[:-2] and not add_flag:
                if (i > 0 and not e_flag) or (e_flag and s[i - 1] not in symbol_arr[2:]):
                    flag = False
                    break
                add_flag = True
            elif add_flag:
                if s[i] in num_arr[:-1]:
                    add_flag = False
                    num_flag = True
                elif s[i] == ".":
                    add_flag = False
                    continue
                else:
                    flag = False
                if e_flag and not add_flag:
                    flag = True
            elif e_flag:
                if s[i] not in num_arr[:-1]:
                    flag = False
                    break
                elif s[i - 1] in symbol_arr[2:] and not flag:
                    flag = True
            elif s[i] == ".":
                if point_flag or has_point:
                    flag = False
                    break
                if not num_flag:
                    point_flag = True
                    flag = False
                has_point = True
            elif point_flag:
                if s[i] in num_arr:
                    num_flag = True
                else:
                    break
                if num_flag:
                    point_flag = False
                    flag = True
            elif s[i] in num_arr[:-1]:
                num_flag = True
                if e_flag:
                    flag = True
            elif s[i] in symbol_arr[2:] and not e_flag:
                e_flag = True
                flag = False
                if i == 0:
                    break
            if s[i] not in num_arr[:-1]:
                num_flag = False
            if s[i] not in num_arr and s[i] not in symbol_arr:
                flag = False
            if not flag and not e_flag and not point_flag:
                break
            i += 1
        return flag

    def isNumber(self, s: str) -> bool:
        State = Enum("State", [
            "STATE_INITIAL",
            "STATE_INT_SIGN",
            "STATE_INTEGER",
            "STATE_POINT",
            "STATE_POINT_WITHOUT_INT",
            "STATE_FRACTION",
            "STATE_EXP",
            "STATE_EXP_SIGN",
            "STATE_EXP_NUMBER",
            "STATE_END"
        ])
        Chartype = Enum("Chartype", [
            "CHAR_NUMBER",
            "CHAR_EXP",
            "CHAR_POINT",
            "CHAR_SIGN",
            "CHAR_ILLEGAL"
        ])

        def toChartype(ch: str) -> Chartype:
            if ch.isdigit():
                return Chartype.CHAR_NUMBER
            elif ch.lower() == "e":
                return Chartype.CHAR_EXP
            elif ch == ".":
                return Chartype.CHAR_POINT
            elif ch == "+" or ch == "-":
                return Chartype.CHAR_SIGN
            else:
                return Chartype.CHAR_ILLEGAL

        transfer = {
            State.STATE_INITIAL: {
                Chartype.CHAR_NUMBER: State.STATE_INTEGER,
                Chartype.CHAR_POINT: State.STATE_POINT_WITHOUT_INT,
                Chartype.CHAR_SIGN: State.STATE_INT_SIGN
            },
            State.STATE_INT_SIGN: {
                Chartype.CHAR_NUMBER: State.STATE_INTEGER,
                Chartype.CHAR_POINT: State.STATE_POINT_WITHOUT_INT
            },
            State.STATE_INTEGER: {
                Chartype.CHAR_NUMBER: State.STATE_INTEGER,
                Chartype.CHAR_EXP: State.STATE_EXP,
                Chartype.CHAR_POINT: State.STATE_POINT
            },
            State.STATE_POINT: {
                Chartype.CHAR_NUMBER: State.STATE_FRACTION,
                Chartype.CHAR_EXP: State.STATE_EXP
            },
            State.STATE_POINT_WITHOUT_INT: {
                Chartype.CHAR_NUMBER: State.STATE_FRACTION
            },
            State.STATE_FRACTION: {
                Chartype.CHAR_NUMBER: State.STATE_FRACTION,
                Chartype.CHAR_EXP: State.STATE_EXP
            },
            State.STATE_EXP: {
                Chartype.CHAR_NUMBER: State.STATE_EXP_NUMBER,
                Chartype.CHAR_SIGN: State.STATE_EXP_SIGN
            },
            State.STATE_EXP_SIGN: {
                Chartype.CHAR_NUMBER: State.STATE_EXP_NUMBER
            },
            State.STATE_EXP_NUMBER: {
                Chartype.CHAR_NUMBER: State.STATE_EXP_NUMBER
            },
        }

        st = State.STATE_INITIAL
        for ch in s:
            typ = toChartype(ch)
            if typ not in transfer[st]:
                return False
            st = transfer[st][typ]

        return st in [State.STATE_INTEGER, State.STATE_POINT, State.STATE_FRACTION, State.STATE_EXP_NUMBER,
                      State.STATE_END]

if __name__ == '__main__':
    # s = "0"
    sa = ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]
    sa = ["12345.", "123.312", "1.1", "1.123123213213123"]
    sa = ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]
    # sa = ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]
    # sa = ["."]
    # sa = ["0.."]
    # sa = ["3."]
    # sa = [".e1"]
    # sa = ["11234.1."]
    # sa = ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]
    # sa = ["2e0", ".e1", "+.", "+E3", "7e69e"]
    # sa = ["7e69e"]

    num_arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."]

    sa = ["7e3+"]

    # print(num_arr[:-1])
    for s in sa:
        print(Solution().isNumber(s))
    # search("(\d{1,}.|\d{1,}.\d{1,}|.\d{1,}|\d{1,})", "123.123")
    # # s = " 123 "
    # # print(s[1:-1])
    #
    # str = "   hello word  "
    #
    # print(str[1:])
    #
    # left, right = 0,  len(str) - 1
    # while left < len(str) and str[left] == " ":
    #     left += 1
    # while right > 0 and str[right] == " ":
    #     right -= 1
    # print(str[left:right + 1])

66. 加一

难度简单904

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。

示例 2:

输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。

示例 3:

输入:digits = [0]
输出:[1]

提示:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

通过次数426,939

提交次数923,900

# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time    : 2022/1/20 16:49 
# @Author  : mtl
# @Desc    : ***
# @File    : 66.py
# @Software: PyCharm
from typing import List


class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        dlen = len(digits)
        def add(i):
            if -i > dlen:
                digits.insert(0, 1)
            elif digits[i] + 1 == 10:
                digits[i] = 0
                add(i - 1)
            else:
                digits[i] += 1
            return digits
        return add(-1)

    def plusOne2(self, digits: List[int]) -> List[int]:
        right = len(digits) - 1
        while right >= -1:
            if right == -1:
                digits.insert(0, 1)
                break
            digits[right] += 1
            if digits[right] == 10:
                digits[right] = 0
            else:
                break
            right -= 1
        return digits

    def plusOne(self, digits: List[int]) -> List[int]:
        digits[-1] += 1
        for d in range(len(digits) - 1, -1, -1):
            if digits[d] == 10:
                digits[d] = 0
                if d - 1 < 0:
                    digits.insert(0, 1)
                else:
                    digits[d - 1] += 1
            else:
                break
        return digits

if __name__ == '__main__':
    digits = [9, 9, 8]
    # print(digits[:-1])
    print(Solution().plusOne(digits))