AcWing 31. 【导师和面试官看了想把你从窗户抛出去系列】之表示数值的字符串
原题链接
困难
作者:
理想不大
,
2021-03-10 15:29:27
,
所有人可见
,
阅读 346
C++ 代码
class Solution {
public:
bool isNumber(string s) {
int countE = 0;
int countP = 0;
int countO = 0;
int count = 0;
int indexE[100];
int indexP[100];
int indexO[100];
if (s[0] == 'e' || s[0] == 'E') return 0;
if (s[0] == '.' && (s[1] == 'e' || s[1] == 'E') ) return 0;
if (s[s.size()-1] == 'e') return 0;
for (int i = 0; i < s.size(); i++)
{
if ( !( ( s[i] <= '9' && s[i] >= '0') || s[i] == '+' || s[i] == '-' || s[i] == 'E' || s[i] == 'e' || s[i] == '.' ))return 0;
if ( s[i] == '+' || s[i] == '-') indexO[countO++] = i;
if ( s[i] == 'E' || s[i] == 'e') indexE[countE++] = i;
if ( s[i] == '.') indexP[countP++] = i;
if ( (s[i] == '+' || s[i] =='-' ) && (s[i+1] == '+' || s[i+1] =='-')) return 0;
if ( countP > 1 || countE > 1) return 0;
count++;
}
if ( count - countE - countP - countO ==0) return 0;
if ( countO == count || countE == count || countP == count) return 0 ;
if ( countO && countP && countE && (indexP[countP - 1] > indexE[countE - 1]) ) return 0;
if ( !countE && countO && indexO[0] != 0) return 0;
return 1;
}
};