【ブラシ問題】【C++】Valid Number問題
leetcode元アドレス:https://leetcode.com/problems/valid-number/
原題:
Validate if a given string is numeric.
Some examples:
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Update (2015-02-10): The signature of the
原題:
Validate if a given string is numeric.
Some examples:
"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Update (2015-02-10): The signature of the
C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition. enNumber(string s){
// , , ,e , ,
int count = 0,pointCount = 0,eCount = 0,spaceCount = 0,negCount = 0;
// , , ,e ,
int firstNum,lastNum,eNum = s.length(),pointNum = -1;
//
for (int i = 0; i < s.length() ; i++)
{
if (s[i] != ' '){
firstNum = i;
break;
}
else{
spaceCount++;
}
}
// false
if (spaceCount == s.length())
return false;
//
for (int i = s.length() - 1; i >= 0; i--){
if (s[i] != ' '){
lastNum = i;
break;
}
}
//
string subString (s, firstNum, lastNum - firstNum + 1);
//
for (int i = 0; i < subString.length(); i++){
// 1
if (subString[i] >= '0'&&subString[i] <= '9'){
count++;
}
// ,
else if (subString[i] == '.')
{
pointNum = i;
pointCount++;
}
// e , e
else if (subString[i] == 'e')
{
eCount++;
if (i1 && subString[i - 1] == 'e'&&(subString.length()-1)>i)
negCount++;
}
}
// 1 , , 0 e , 0-9
if (pointCount == 1 && count > 0 && pointNum= '0'))
{
count++;
}
}
// 1, , 0-9 , 0-9 e
else if ((subString[pointNum - 1] >= '0'&&subString[pointNum - 1] <= '9') || (subString[pointNum + 1] >= '0'&&subString[pointNum + 1] <= '9')||subString[pointNum+1]=='e')
count++;
}
// e 1, 0, e , e
if (eCount == 1 && count > 0&&eNum > 0&&eNum 0&&subString[eNum-1]!='-'&&subString[eNum-1]!='+')
count++;
// 0-2
if (negCount >= 0 && negCount <= 2)
count += negCount;
// , ,
if (count == subString.length())
return true;
else
return false;
}