第一题用Perl regular expression可以一行搞定。 if(str =~ /[+|-]?[0|[1-9]\d*](\.[0-9]+)*/ || str =~ /[+|-]?\.\d+/) return true; else return false; 如果不能用regular expression,下面有个C++的code。 bool isNumeric(string str) { // assume there are no leading or ending spaces in the string int i = 0, end = str.size()-1; if(str[i] == '+' || str[i] == '-') i++; // check the sign symbol
if(str[i] == '.') { // if the first non-sign char is '.', e.g. .56 if(i == end) return false; // not numeric if no digits after the '.' symbol i++; } else if(str[i] == '0') { // if the first non-sign char is 0, e.g. 0. 45 if(i == end) return true; // check special cases: +0, -0, 0 i++; if(str[i] != '.' || i == end) return false; // not numeric if no digits after the '.' symbol else i++; } else if(str[i] > 48 && str[i] <= 57) { // if the first non-sign char is 1~9 i++; while(i <= end) { if(str[i] == '.') { if(i == end) return false; // not numeric if no digits after the '.' symbol i++; break; } else if(str[i] < 48 || str[i] > 57) return false; i++; } } else return false;
while(i <= end) { // process the digits after the '.' symbol if(str[i] < 48 && str[i] > 57) return false; i++; } return true; }
z*3
32 楼
是否是数字,一行就够咯 Double.parseDouble(String);
z*3
33 楼
尼玛,谁在挖坟?
p*2
34 楼
我觉得leetcode上边这题很坑爹。
【在 w****x 的大作中提到】 : : leetcode那题几乎是我见到过最难的了
c*p
35 楼
mark
r*c
36 楼
二爷,坑爹算法来也。用状态机搞砸一切面面!哈哈 #include #include #include using namespace std; class Numericality; void TestNumericality(); void TestNumericalityHelper(const string& str, const bool expected, Numericality& nc); class Numericality { public: struct State { const string _str; int _index; State() {} State(string s, int i) : _str(s), _index(i){} virtual State* MoveNext(){ if(_str.empty() || _index < 0 || _index >= _str.length()) return new RejectState(); if(_str.at(_index) == '+' || _str.at(_index) == '-') return new SignState(_str, _index+1); else if(_str.at(_index) == '.') return new FirstDotState(_str, _index); else if(isdigit( _str.at(_index))) return new DigitPlusState(_str, _index+1); else return new RejectState(); } virtual bool isRejected(){return false;} }; struct RejectState : public State { RejectState(): State(){} State* MoveNext(){return 0;} bool isRejected(){return true;} }; struct AcceptState : public State { AcceptState(): State(){} State* MoveNext(){return 0;} }; struct SignState : public State { SignState(string s, int i) : State(s, i){} State* MoveNext(){ if(_str.at(_index) == '.') return new FirstDotState(_str, _index); else if(isdigit( _str.at(_index))) return new DigitPlusState(_str, _index+1); else return new RejectState(); } }; struct FirstDotState : public State { FirstDotState(string s, int i):State(s, i){} State* MoveNext(){ if(_str.length() == 2) return new RejectState(); else if(isdigit( _str.at(_index + 1))) return new DigitPlusState(_str, _index+1); else return new RejectState(); } }; struct SecondDotState : public State { SecondDotState(string s, int i):State(s, i){} State* MoveNext(){ if(isdigit( _str.at(_index))) return new DigitPlusState(_str, _index+1); else return new RejectState(); } }; struct DigitPlusState : public State { DigitPlusState(string s, int i) : State(s, i){} State* MoveNext(){ if(_index == _str.length()) //all char used up return new AcceptState(); else if(isdigit( _str.at(_index))) return new DigitPlusState(_str, _index+1); else if(_str.at(_index) == '.') return new SecondDotState(_str, _index + 1); else return new RejectState(); } }; bool isNumeric(string str) { State *pState = new State(str, 0); while(pState->_index <= str.length()-1) { State *pTemp = pState; pState = pState->MoveNext(); delete pTemp; if(pState->isRejected()) return false; } if(pState->isRejected()) return false; return true; } bool isNumeric_mitbbs(string str) //unfortunately this is a buggie function { // assume there are no leading or ending spaces in the string int i = 0, end = str.size()-1; if(str[i] == '+' || str[i] == '-') i++; // check the sign symbol if(str[i] == '.') { // if the first non-sign char is '.', e.g. . 56 if(i == end) return false; // not numeric if no digits after the '.' symbol i++; } else if(str[i] == '0') { // if the first non-sign char is 0, e.g . 0.45 if(i == end) return true; // check special cases: +0, -0, 0 i++; if(str[i] != '.' || i == end) return false; // not numeric if no digits after the '.' symbol else i++; } else if(str[i] > 48 && str[i] <= 57) { // if the first non-sign char is 1~9 i++; while(i <= end) { if(str[i] == '.') { if(i == end) return false; // not numeric if no digits after the '.' symbol i++; break; } else if(str[i] < 48 || str[i] > 57) return false; i++; } } else return false; while(i <= end) { // process the digits after the '.' symbol if(str[i] < 48 && str[i] > 57) return false; i++; } return true; } }; void TestNumericality() { Numericality nc; string str("+2345"); TestNumericalityHelper(str, true, nc); assert(nc.isNumeric("+2345")); str = "-2345"; TestNumericalityHelper(str, true, nc); str = "2345.2345"; TestNumericalityHelper(str, true, nc); str = "-.2345.2345"; TestNumericalityHelper(str, true, nc); str = "+.987.123"; TestNumericalityHelper(str, true, nc); str = "-..987.123"; TestNumericalityHelper(str, false, nc); str = "-93j123"; TestNumericalityHelper(str, false, nc); str = "-76532."; TestNumericalityHelper(str, false, nc); str = "2345.23.5"; TestNumericalityHelper(str, false, nc); } void TestNumericalityHelper(const string& str, const bool expected, Numericality& nc) { cout<"false")<bool result = nc.isNumeric(str); bool result_mitbbs = nc.isNumeric_mitbbs(str); cout<result_mitbbs ? "true" : "false")<}