Problem Description
Validate if a given string is numeric.Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => trueNote: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Some Details
‘numeric’ is the key of this problem, also the reason of it being the most difficult problem to solve in leetcode.You need to make a list before coding, about how to define the number.
Solution
I finish this problem by making this list:
Different bases. Your code need to distinguish data in binary, octal, decimal and hex formats.Scientific notation. ‘2e10’ as an example to show it. pic below. Space. Ignore if the string has space before the number of after the number.Symbol. ‘+”-’ is only allowed once before the number(twice in scientific notation because 2 numbers in scientific notation in fact).Decimal point. ‘.’ is only allowed once in the middle of the number.At least one digit. For example, ‘+’ , ‘.’ is not a number but ‘+1’ , ‘.0’ is OK.
In fact, this list is more stringent than the requirements of the problem.
Bases are unnecessary to discuss in this problem. Specifically, format in binary system and octal system is similar to it is in decimal system. What’s more, ‘abc’ as an example to show hex format is not allowed in the problem. The problem doesn’t follow strict rules in Scientific notation. Look at the pic below, if you judge whether A is in range of [1,10), you can’t pass this problem.
It’s not really easy to accept this problem, so you could find more details in my code. Here are many hints in Chinese in my code.
Code
class Solution {
public:
bool binary(
string s,
int l,
int r)
{
return false;
}
bool oct(
string s,
int l,
int r)
{
return false;
}
bool dec(
string s,
int l,
int r)
{
bool p=
false;
int pn=-
1;
for (
int i=l;i<=r;i++)
{
if (s[i]==
'.')
{
if (!p)
{
p=
true; pn=i;
}
else return false;
}
}
bool eq=
false;
for (
int i=l;i<=r;i++)
{
if (s[i]>=
'0' && s[i]<=
'9')
{
eq=
true;
}
else
{
if (i==l && (s[i]==
'+' || s[i]==
'-'));
else
if (p && i==pn && s[i]==
'.');
else
return false;
}
}
return eq;
}
bool hex(
string s,
int l,
int r)
{
return false;
}
bool sci(
string s,
int l,
int r)
{
bool e=
false;
int en=-
1;
for (
int i=l;i<=r;i++)
{
if (s[i]==
'e')
{
if (!e)
{
e=
true; en=i;
}
else return false;
}
}
if (!e)
return false;
if (l==en || en==r)
return false;
if (!dec(s,l,en-
1))
return false;
int pn=en;
for (
int i=l;i<en;i++)
if (s[i]==
'.')
{
pn=i;
break;
}
int num=
0,now=
1;
bool eq=
false;
for (
int i=pn-
1;i>=l;i--)
{
if (s[i]>=
'0' && s[i]<=
'9')
{
eq=
true;
num=num+now*(s[i]-
'0'); now*=
10;
}
else;
}
eq=
false;
for (
int i=en+
1;i<=r;i++)
{
if (s[i]>=
'0' && s[i]<=
'9')
{
eq=
true;
}
else
{
if (i==en+
1 &&(s[i]==
'+'||s[i]==
'-'));
else
return false;
}
}
return eq;
}
bool isNumber(
string s)
{
int len=s.length(),l=
0,r=len-
1;
while (s[l]==
' ' && l<len) l++;
while (s[r]==
' ' && r>=
0) r--;
if (l>r)
return false;
if (binary(s,l,r))
return true;
else
if (oct(s,l,r))
return true;
else
if (dec(s,l,r))
return true;
else
if (hex(s,l,r))
return true;
else
if (sci(s,l,r))
return true;
else
return false;
}
};
转载请注明原文地址: https://ju.6miu.com/read-1124405.html