题目描述 有效数字(按顺序)可以分成以下几个部分:
一个 小数 或者 整数
(可选)一个 ‘e’ 或 ‘E’ ,后面跟着一个 整数
小数(按顺序)可以分成以下几个部分:
(可选)一个符号字符(’+’ 或 ‘-‘)
下述格式之一:
至少一位数字,后面跟着一个点 ‘.’
至少一位数字,后面跟着一个点 ‘.’ ,后面再跟着至少一位数字
一个点 ‘.’ ,后面跟着至少一位数字
整数(按顺序)可以分成以下几个部分:
(可选)一个符号字符(’+’ 或 ‘-‘)
至少一位数字
部分有效数字列举如下:[“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
提示:
1 <= s.length <= 20
s 仅含英文字母(大写和小写),数字(0-9),加号 ‘+’ ,减号 ‘-‘ ,或者点 ‘.’ 。
解决方法 C++ 1 2 3 4 5 6 7 8 9 10 class Solution {public : bool isNumber (string s) { if (s.length () < 1 || s.length () > 20 ) { return false ; } regex validNumberRegex ("^[-+]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][-+]?[0-9]+)?$" ) ; return regex_match (s, validNumberRegex); } };
结果 执行用时 : 37 ms, 击败 8.95% 使用 C++ 的用户
内存消耗 : 24.11 MB, 击败 7.88% 使用 C++ 的用户
Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 class Solution { public boolean isNumber (String s) { Map<State, Map<CharType, State>> transfer = new HashMap <State, Map<CharType, State>>(); Map<CharType, State> initialMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.INTEGER); put(CharType.POINTC, State.POINT_WITHOUT_INT); put(CharType.SIGN, State.INT_SIGN); }}; transfer.put(State.INITIAL, initialMap); Map<CharType, State> intSignMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.INTEGER); put(CharType.POINTC, State.POINT_WITHOUT_INT); }}; transfer.put(State.INT_SIGN, intSignMap); Map<CharType, State> integerMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.INTEGER); put(CharType.EXPC, State.EXP); put(CharType.POINTC, State.POINT); }}; transfer.put(State.INTEGER, integerMap); Map<CharType, State> pointMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.FRACTION); put(CharType.EXPC, State.EXP); }}; transfer.put(State.POINT, pointMap); Map<CharType, State> pointWithoutIntMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.FRACTION); }}; transfer.put(State.POINT_WITHOUT_INT, pointWithoutIntMap); Map<CharType, State> fractionMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.FRACTION); put(CharType.EXPC, State.EXP); }}; transfer.put(State.FRACTION, fractionMap); Map<CharType, State> expMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.EXP_NUMBER); put(CharType.SIGN, State.EXP_SIGN); }}; transfer.put(State.EXP, expMap); Map<CharType, State> expSignMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.EXP_NUMBER); }}; transfer.put(State.EXP_SIGN, expSignMap); Map<CharType, State> expNumberMap = new HashMap <CharType, State>() {{ put(CharType.NUMBER, State.EXP_NUMBER); }}; transfer.put(State.EXP_NUMBER, expNumberMap); int length = s.length(); State state = State.INITIAL; for (int i = 0 ; i < length; i++) { CharType type = toCharType(s.charAt(i)); if (!transfer.get(state).containsKey(type)) { return false ; } else { state = transfer.get(state).get(type); } } return state == State.INTEGER || state == State.POINT || state == State.FRACTION || state == State.EXP_NUMBER || state == State.END; } public CharType toCharType (char ch) { if (ch >= '0' && ch <= '9' ) { return CharType.NUMBER; } else if (ch == 'e' || ch == 'E' ) { return CharType.EXPC; } else if (ch == '.' ) { return CharType.POINTC; } else if (ch == '+' || ch == '-' ) { return CharType.SIGN; } else { return CharType.ILLEGAL; } } enum State { INITIAL, INT_SIGN, INTEGER, POINT, POINT_WITHOUT_INT, FRACTION, EXP, EXP_SIGN, EXP_NUMBER, END } enum CharType { NUMBER, EXPC, POINTC, SIGN, ILLEGAL } }
结果 执行用时 : 5 ms, 击败 30.97% 使用 Java 的用户
内存消耗 : 43.59 MB, 击败 16.79% 使用 Java 的用户
Python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 class Solution (object ): def isNumber (self, s ): """ :type s: str :rtype: bool """ transition = { State.INITIAL: {CharType.NUMBER: State.INTEGER, CharType.POINTC: State.POINT_WITHOUT_INT, CharType.SIGN: State.INT_SIGN}, State.INT_SIGN: {CharType.NUMBER: State.INTEGER, CharType.POINTC: State.POINT_WITHOUT_INT}, State.INTEGER: {CharType.NUMBER: State.INTEGER, CharType.EXPC: State.EXP, CharType.POINTC: State.POINT}, State.POINT: {CharType.NUMBER: State.FRACTION, CharType.EXPC: State.EXP}, State.POINT_WITHOUT_INT: {CharType.NUMBER: State.FRACTION}, State.FRACTION: {CharType.NUMBER: State.FRACTION, CharType.EXPC: State.EXP}, State.EXP: {CharType.NUMBER: State.EXP_NUMBER, CharType.SIGN: State.EXP_SIGN}, State.EXP_SIGN: {CharType.NUMBER: State.EXP_NUMBER}, State.EXP_NUMBER: {CharType.NUMBER: State.EXP_NUMBER} } len_s = len (s) state = State.INITIAL for i in range (len_s): char_type = self .get_char_type(s[i]) if char_type not in transition[state]: return False else : state = transition[state][char_type] return state in {State.INTEGER, State.POINT, State.FRACTION, State.EXP_NUMBER, State.END} def get_char_type (self, ch ): if ch.isdigit(): return CharType.NUMBER elif ch == "." : return CharType.POINTC elif ch == "+" or ch == "-" : return CharType.SIGN elif ch.lower() == "e" : return CharType.EXPC else : return CharType.ILLEGAL class State : INITIAL = "INITIAL" INT_SIGN = "INT_SIGN" INTEGER = "INTEGER" POINT = "POINT" POINT_WITHOUT_INT = "POINT_WITHOUT_INT" FRACTION = "FRACTION" EXP = "EXP" EXP_SIGN = "EXP_SIGN" EXP_NUMBER = "EXP_NUMBER" END = "END" class CharType : NUMBER = "NUMBER" EXPC = "EXPC" POINTC = "POINTC" SIGN = "SIGN" ILLEGAL = "ILLEGAL"
结果 执行用时 : 26 ms, 击败 69.23% 使用 Python 的用户
内存消耗 : 11.41 MB, 击败 76.92% 使用 Python 的用户
Python3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 class Solution : def isNumber (self, s: str ) -> bool : transition = { State.INITIAL: {CharType.NUMBER: State.INTEGER, CharType.POINTC: State.POINT_WITHOUT_INT, CharType.SIGN: State.INT_SIGN}, State.INT_SIGN: {CharType.NUMBER: State.INTEGER, CharType.POINTC: State.POINT_WITHOUT_INT}, State.INTEGER: {CharType.NUMBER: State.INTEGER, CharType.EXPC: State.EXP, CharType.POINTC: State.POINT}, State.POINT: {CharType.NUMBER: State.FRACTION, CharType.EXPC: State.EXP}, State.POINT_WITHOUT_INT: {CharType.NUMBER: State.FRACTION}, State.FRACTION: {CharType.NUMBER: State.FRACTION, CharType.EXPC: State.EXP}, State.EXP: {CharType.NUMBER: State.EXP_NUMBER, CharType.SIGN: State.EXP_SIGN}, State.EXP_SIGN: {CharType.NUMBER: State.EXP_NUMBER}, State.EXP_NUMBER: {CharType.NUMBER: State.EXP_NUMBER} } len_s = len (s) state = State.INITIAL for i in range (len_s): char_type = self .get_char_type(s[i]) if char_type not in transition[state]: return False else : state = transition[state][char_type] return state in {State.INTEGER, State.POINT, State.FRACTION, State.EXP_NUMBER, State.END} def get_char_type (self, ch ): if ch.isdigit(): return CharType.NUMBER elif ch == "." : return CharType.POINTC elif ch == "+" or ch == "-" : return CharType.SIGN elif ch.lower() == "e" : return CharType.EXPC else : return CharType.ILLEGAL class State : INITIAL = "INITIAL" INT_SIGN = "INT_SIGN" INTEGER = "INTEGER" POINT = "POINT" POINT_WITHOUT_INT = "POINT_WITHOUT_INT" FRACTION = "FRACTION" EXP = "EXP" EXP_SIGN = "EXP_SIGN" EXP_NUMBER = "EXP_NUMBER" END = "END" class CharType : NUMBER = "NUMBER" EXPC = "EXPC" POINTC = "POINTC" SIGN = "SIGN" ILLEGAL = "ILLEGAL"
结果 执行用时 : 37 ms, 击败 85.06% 使用 Python3 的用户
内存消耗 : 16.50 MB, 击败 37.66% 使用 Python3 的用户
C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 enum State { INITIAL, INT_SIGN, INTEGER, POINT, POINT_WITHOUT_INT, FRACTION, EXP, EXP_SIGN, EXP_NUMBER, END, ILLEGAL }; enum CharType { NUMBER, EXPC, POINTC, SIGN, ILLEGALC }; enum CharType getCharType (char ch) { if (ch >= '0' && ch <= '9' ) { return NUMBER; } else if (ch == 'e' || ch == 'E' ) { return EXPC; } else if (ch == '.' ) { return POINTC; } else if (ch == '+' || ch == '-' ) { return SIGN; } else { return ILLEGALC; } } enum State transfer (enum State st, enum CharType typ) { switch (st) { case INITIAL: { switch (typ) { case NUMBER: return INTEGER; case POINTC: return POINT_WITHOUT_INT; case SIGN: return INT_SIGN; default : return ILLEGAL; } } case INT_SIGN: { switch (typ) { case NUMBER: return INTEGER; case POINTC: return POINT_WITHOUT_INT; default : return ILLEGAL; } } case INTEGER: { switch (typ) { case NUMBER: return INTEGER; case EXPC: return EXP; case POINTC: return POINT; default : return ILLEGAL; } } case POINT: { switch (typ) { case NUMBER: return FRACTION; case EXPC: return EXP; default : return ILLEGAL; } } case POINT_WITHOUT_INT: { switch (typ) { case NUMBER: return FRACTION; default : return ILLEGAL; } } case FRACTION: { switch (typ) { case NUMBER: return FRACTION; case EXPC: return EXP; default : return ILLEGAL; } } case EXP: { switch (typ) { case NUMBER: return EXP_NUMBER; case SIGN: return EXP_SIGN; default : return ILLEGAL; } } case EXP_SIGN: { switch (typ) { case NUMBER: return EXP_NUMBER; default : return ILLEGAL; } } case EXP_NUMBER: { switch (typ) { case NUMBER: return EXP_NUMBER; default : return ILLEGAL; } } default : return ILLEGAL; } } bool isNumber (char * s) { int len = strlen (s); enum State st = INITIAL; for (int i = 0 ; i < len; i++) { enum CharType typ = getCharType(s[i]); enum State nextState = transfer(st, typ); if (nextState == ILLEGAL) return false ; st = nextState; } return st == INTEGER || st == POINT || st == FRACTION || st == EXP_NUMBER || st == END; }
结果 执行用时 : 0 ms, 击败 100.00% 使用 C 的用户
内存消耗 : 5.52 MB, 击败 89.66% 使用 C 的用户
C# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 public class Solution { public bool IsNumber (string s ) { Dictionary<State, Dictionary<CharType, State>> transfer = new Dictionary<State, Dictionary<CharType, State>>(); Dictionary<CharType, State> initialDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.INTEGER}, {CharType.POINTC, State.POINT_WITHOUT_INT}, {CharType.SIGN, State.INT_SIGN} }; transfer.Add(State.INITIAL, initialDictionary); Dictionary<CharType, State> intSignDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.INTEGER}, {CharType.POINTC, State.POINT_WITHOUT_INT} }; transfer.Add(State.INT_SIGN, intSignDictionary); Dictionary<CharType, State> integerDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.INTEGER}, {CharType.EXPC, State.EXP}, {CharType.POINTC, State.POINT} }; transfer.Add(State.INTEGER, integerDictionary); Dictionary<CharType, State> pointDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.FRACTION}, {CharType.EXPC, State.EXP} }; transfer.Add(State.POINT, pointDictionary); Dictionary<CharType, State> pointWithoutIntDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.FRACTION} }; transfer.Add(State.POINT_WITHOUT_INT, pointWithoutIntDictionary); Dictionary<CharType, State> fractionDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.FRACTION}, {CharType.EXPC, State.EXP} }; transfer.Add(State.FRACTION, fractionDictionary); Dictionary<CharType, State> expDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.EXP_NUMBER}, {CharType.SIGN, State.EXP_SIGN} }; transfer.Add(State.EXP, expDictionary); Dictionary<CharType, State> expSignDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.EXP_NUMBER} }; transfer.Add(State.EXP_SIGN, expSignDictionary); Dictionary<CharType, State> expNumberDictionary = new Dictionary<CharType, State> { {CharType.NUMBER, State.EXP_NUMBER} }; transfer.Add(State.EXP_NUMBER, expNumberDictionary); int length = s.Length; State state = State.INITIAL; for (int i = 0 ; i < length; i++) { CharType type = GetCharType(s[i]); if (!transfer[state].ContainsKey(type)) { return false ; } else { state = transfer[state][type]; } } return state == State.INTEGER || state == State.POINT || state == State.FRACTION || state == State.EXP_NUMBER || state == State.END; } private CharType GetCharType (char ch ) { if (ch >= '0' && ch <= '9' ) { return CharType.NUMBER; } else if (ch == 'e' || ch == 'E' ) { return CharType.EXPC; } else if (ch == '.' ) { return CharType.POINTC; } else if (ch == '+' || ch == '-' ) { return CharType.SIGN; } else { return CharType.ILLEGAL; } } private enum State { INITIAL, INT_SIGN, INTEGER, POINT, POINT_WITHOUT_INT, FRACTION, EXP, EXP_SIGN, EXP_NUMBER, END } private enum CharType { NUMBER, EXPC, POINTC, SIGN, ILLEGAL } }
结果 执行用时 : 66 ms, 击败 50.00% 使用 C# 的用户
内存消耗 : 46.64 MB, 击败 6.67% 使用 C# 的用户
JavaScript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 var isNumber = function (s ) { const State = { INITIAL : 'INITIAL' , INT_SIGN : 'INT_SIGN' , INTEGER : 'INTEGER' , POINT : 'POINT' , POINT_WITHOUT_INT : 'POINT_WITHOUT_INT' , FRACTION : 'FRACTION' , EXP : 'EXP' , EXP_SIGN : 'EXP_SIGN' , EXP_NUMBER : 'EXP_NUMBER' , END : 'END' }; const CharType = { NUMBER : 'NUMBER' , EXP : 'EXP' , POINT : 'POINT' , SIGN : 'SIGN' , ILLEGAL : 'ILLEGAL' }; const transition = { [State .INITIAL ]: { [CharType .NUMBER ]: State .INTEGER , [CharType .POINT ]: State .POINT_WITHOUT_INT , [CharType .SIGN ]: State .INT_SIGN }, [State .INT_SIGN ]: { [CharType .NUMBER ]: State .INTEGER , [CharType .POINT ]: State .POINT_WITHOUT_INT }, [State .INTEGER ]: { [CharType .NUMBER ]: State .INTEGER , [CharType .EXP ]: State .EXP , [CharType .POINT ]: State .POINT }, [State .POINT ]: { [CharType .NUMBER ]: State .FRACTION , [CharType .EXP ]: State .EXP }, [State .POINT_WITHOUT_INT ]: { [CharType .NUMBER ]: State .FRACTION }, [State .FRACTION ]: { [CharType .NUMBER ]: State .FRACTION , [CharType .EXP ]: State .EXP }, [State .EXP ]: { [CharType .NUMBER ]: State .EXP_NUMBER , [CharType .SIGN ]: State .EXP_SIGN }, [State .EXP_SIGN ]: { [CharType .NUMBER ]: State .EXP_NUMBER }, [State .EXP_NUMBER ]: { [CharType .NUMBER ]: State .EXP_NUMBER } }; const getCharType = (ch ) => { if (/\d/ .test (ch)) { return CharType .NUMBER ; } else if (ch === 'e' || ch === 'E' ) { return CharType .EXP ; } else if (ch === '.' ) { return CharType .POINT ; } else if (ch === '+' || ch === '-' ) { return CharType .SIGN ; } else { return CharType .ILLEGAL ; } }; let state = State .INITIAL ; for (const char of s) { const charType = getCharType (char); if (!transition[state][charType]) { return false ; } else { state = transition[state][charType]; } } return state === State .INTEGER || state === State .POINT || state === State .FRACTION || state === State .EXP_NUMBER || state === State .END ; };
结果 执行用时 : 97 ms, 击败 19.42% 使用 JavaScript 的用户
内存消耗 : 54.57 MB, 击败 7.76% 使用 JavaScript 的用户
TypeScript 1 2 3 4 5 6 7 function isNumber (s : string ): boolean { if (s.length < 1 || s.length > 20 ) { return false ; } const validNumberRegex = /^[\+\-]?(\d+(\.\d*)?|\.\d+)([eE][\+\-]?\d+)?$/ ; return validNumberRegex.test (s); }
结果 执行用时 : 88 ms, 击败 57.14% 使用 TypeScript 的用户
内存消耗 : 53.75 MB, 击败 66.67% 使用 TypeScript 的用户
PHP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { function isNumber ($s ) { if (strlen ($s ) < 1 || strlen ($s ) > 20 ) { return false ; } $validNumberRegex = '/^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/' ; return preg_match ($validNumberRegex , $s ) === 1 ; } }
结果 执行用时 : 9 ms, 击败 100.00% 使用 PHP 的用户
内存消耗 : 20.38 MB, 击败 100.00% 使用 PHP 的用户
Swift 1 2 3 4 5 6 7 8 9 class Solution { func isNumber (_ s : String ) -> Bool { if s.count < 1 || s.count > 20 { return false } let validNumberRegex = #"^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$"# return s.range(of: validNumberRegex, options: .regularExpression) != nil } }
结果 执行用时 : 12 ms, 击败 100.00% 使用 Swift 的用户
内存消耗 : 18.34 MB, 击败 -% 使用 Swift 的用户
Kotlin 1 2 3 4 5 6 7 8 9 class Solution { fun isNumber (s: String ) : Boolean { if (s.length < 1 || s.length > 20 ) { return false } val validNumberRegex = """^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$""" .toRegex() return validNumberRegex.matches(s) } }
结果 执行用时 : 212 ms, 击败 -% 使用 Kotlin 的用户
内存消耗 : 37.24 MB, 击败 -% 使用 Kotlin 的用户
Dart 1 2 3 4 5 6 class Solution { bool isNumber(String s) { RegExp validNumberRegex = RegExp (r'^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$' ); return validNumberRegex.hasMatch(s); } }
结果 执行用时 : 350 ms, 击败 100.00% 使用 Dart 的用户
内存消耗 : 158.96 MB, 击败 100.00% 使用 Dart 的用户
Go 1 2 3 4 func isNumber (s string ) bool { validNumberRegex := regexp.MustCompile(`^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$` ) return validNumberRegex.MatchString(s) }
结果 执行用时 : 19 ms, 击败 6.45% 使用 Go 的用户
内存消耗 : 7.05 MB, 击败 6.45% 使用 Go 的用户
Ruby 1 2 3 4 5 6 def is_number (s ) valid_number_regex = /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/ !!(s =~ valid_number_regex) end
结果 执行用时 : 56 ms, 击败 100.00% 使用 Ruby 的用户
内存消耗 : 206.57 MB, 击败 100.00% 使用 Ruby 的用户
Scala 暂时未解决
结果 执行用时 : ms, 击败 % 使用 Scala 的用户
内存消耗 : MB, 击败 % 使用 Scala 的用户
Rust 暂时未解决
结果 执行用时 : ms, 击败 % 使用 Rust 的用户
内存消耗 : MB, 击败 % 使用 Rust 的用户
Racket 暂时未解决
结果 执行用时 : ms, 击败 % 使用 Racket 的用户
内存消耗 : MB, 击败 % 使用 Racket 的用户
Erlang 暂时未解决
结果 执行用时 : ms, 击败 % 使用 Erlang 的用户
内存消耗 : MB, 击败 % 使用 Erlang 的用户
Elixir 1 2 3 4 5 6 7 8 9 defmodule Solution do @spec is_number(s :: String .t) :: boolean def is_number (s) do case Regex .match?(~r/^\s *[-+]?(\d +(\. \d *)?|\. \d +)([eE][-+]?\d +)?\s *$/ , s) do true -> true false -> false end end end
结果 执行用时 : 371 ms, 击败 100.00% 使用 Elixir 的用户
内存消耗 : 67.29 MB, 击败 100.00% 使用 Elixir 的用户