# Is the test data created by following the spec IEEE-754?

• Have anyone read the spec IEEE-754 to create the code snippet for this one?

http://en.wikipedia.org/wiki/IEEE_floating_point

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

• Hmmm, I'm not sure. I've seen lots of people complaining about this ambiguous problem, but I think you can get through the ambiguity as long as you remember the following tips:

1. Only those spaces in front of or behind the numbers should be
skipped, others in the midst of the numbers should not, e.g:

`" 11 " -> true, " 1 1 " -> false.`

2. The base is a decimal (including
integer) which should at least have one digit, e.g:

`"1." -> true, ".1" -> true, "." ->false`

3. The exponent is an integer, which follows an exponent mark 'e' or 'E', e.g:

`"1.E+10" -> true, "1.e+1.0" -> false`

My AC code:

``````class Solution {
private:
void skipSpace(const char *&s){
if(!s) return;
while(*s == ' ') s++;
}
bool isSign(const char *s){ return s && *s == '+' || *s == '-'; }
bool isDigit(const char *s){ return s && *s >= '0' && *s <= '9'; }
void skipDigit(const char*&s){
if(!s) return;
while(isDigit(s)) s++;
}
bool isValidDecimalAndSkip(const char *&s){
if(!s) return false;
if(!isSign(s) && !isDigit(s) && *s != '.') return false;
if(isSign(s)) s++;
bool valid = isDigit(s);
skipDigit(s);
if(*s == '.') s++;
valid |= isDigit(s);
skipDigit(s);
return valid;
}
bool isValidExpAndSkip(const char *&s){
if(!s) return false;
if(*s != 'e' && *s != 'E') return true;
s++;
if(isSign(s)) s++;
bool valid = isDigit(s);
skipDigit(s);
return valid;
}
public:
bool isNumber(const char *s) {
if(!s) return false;
skipSpace(s);
if(!isValidDecimalAndSkip(s) || !isValidExpAndSkip(s)) return false;
skipSpace(s);
return !*s;
}
};
``````

My test cases:

``````"0"				true
" 0.1 "			true
"1."			true
".1"			true
"+.E0"			false
"abc"			false
"1 a"			false
"1e10"			true
"2E10"			true
"1e+10"			true
"2e+-10a"		false
"2.1E+1.2"		false``````

• The test data for this problem tests against the standard representation of floats in Java and C/C++. If you want the webpage listing everything required, here it is.

If you really want to make it short, you could just come up with the regular expression for the requirements. The difficult bit here, I think, was meant to be gathering all of the requirements, not checking if they worked for a given number.

``````public boolean isNumber(String s) {
String valid = "\\s*[\\+\\-]?(([0-9]+\\.[0-9]+)|[0-9]+\\.|\\.[0-9]+|([0-9]+))([eE][\\+\\-]?[0-9]+)?\\s*";
return s.matches(valid);
}
``````

• Do we allow spaces between e/E and number? i.e. should "1e 1" return true?

• No, whitespace is only permitted at the edges of the string.

• So why do u have \s* following [eE] in ur regex?

• Apparently because there isn't a test for that... Time to remove it. A question on this site with the same info is here.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.