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.
翻译:判断是否是数字---作弊完成条件太多
Code:
/*
* JavaScript解法:
* 208msAC---https://leetcode.com/discuss/29716/share-my-200ms-one-line-solution-in-javascript
* return ( Number(str)===0 || !!Number(str) ) && ( str.trim() !== "");
*/
/**
* @param {string} s
* @return {boolean}
*/
var isNumber = function(str) {
return ( Number(str)===0 || !!Number(str) ) && ( str.trim() !== "");
};
package From61;
/**
* @author MohnSnow
* @time 2015年6月29日 上午11:22:51
*
*/
public class LeetCode65 {
/**
* @param argsmengdx
* -fnst
*/
//https://leetcode.com/discuss/26682/clear-java-solution-with-ifs
/*
* We start with trimming.
* If we see [0-9] we reset the number flags.
* We can only see . if we didn't see e or ..
* We can only see e if we didn't see e but we did see a number. We reset numberAfterE flag.
* We can only see + and - in the beginning and after an e any other character break the validation.
* At the and it is only valid if there was at least 1 number and if we did see an e then a number after it as well.
* So basically the number should match this regular expression:
* [-+]?[0-9]*(.[0-9]+)?(e[-+]?[0-9]+)?
*/
//328msAC
public static boolean isNumber(String s) {
s = s.trim();
boolean pointSeen = false;
boolean eSeen = false;
boolean numberSeen = false;
boolean numberAfterE = true;
for (int i = 0; i < s.length(); i++) {
if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
numberSeen = true;
numberAfterE = true;
} else if (s.charAt(i) == '.') {
if (eSeen || pointSeen) {
return false;
}
pointSeen = true;
} else if (s.charAt(i) == 'e') {
if (eSeen || !numberSeen) {
return false;
}
numberAfterE = false;
eSeen = true;
} else if (s.charAt(i) == '-' || s.charAt(i) == '+') {
if (i != 0 && s.charAt(i - 1) != 'e') {
return false;
}
} else {
return false;
}
}
return numberSeen && numberAfterE;
}
public static void main(String[] args) {
String s = "1 a";
System.out.println("uniquePaths: " + isNumber(s));
}
}