65. Valid Number
2016-07-19 11:54
411 查看
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.
https://discuss.leetcode.com/topic/1095/the-worst-problem-i-have-ever-met-in-this-oj https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs
一些摘取的测试数据
All we need is to have a couple of flags so we can process the string in linear time:
We start with trimming.
If we see
We can only see
We can only see
we did see a number. We reset numberAfterE flag.
We can only see
the beginning and after an
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
a number after it as well.
So basically the number should match this regular expression:
-----------------------------------------------------------------------------------------------------------------------
正则表达式版本
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.
https://discuss.leetcode.com/topic/1095/the-worst-problem-i-have-ever-met-in-this-oj https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs
一些摘取的测试数据
test(1, "123", true); test(2, " 123 ", true); test(3, "0", true); test(4, "0123", true); //Cannot agree test(5, "00", true); //Cannot agree test(6, "-10", true); test(7, "-0", true); test(8, "123.5", true); test(9, "123.000000", true); test(10, "-500.777", true); test(11, "0.0000001", true); test(12, "0.00000", true); test(13, "0.", true); //Cannot be more disagree!!! test(14, "00.5", true); //Strongly cannot agree test(15, "123e1", true); test(16, "1.23e10", true); test(17, "0.5e-10", true); test(18, "1.0e4.5", false); test(19, "0.5e04", true); test(20, "12 3", false); test(21, "1a3", false); test(22, "", false); test(23, " ", false); test(24, null, false); test(25, ".1", true); //Ok, if you say so test(26, ".", false); test(27, "2e0", true); //Really?! test(28, "+.8", true); test(29, " 005047e+6", true); //Damn = =|||
All we need is to have a couple of flags so we can process the string in linear time:
public 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; }
We start with trimming.
If we see
[0-9]we reset the number flags.
We can only see
.if we didn't see
eor
..
We can only see
eif we didn't see
ebut
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
ethen
a number after it as well.
So basically the number should match this regular expression:
[-+]?[0-9]*(.[0-9]+)?(e[-+]?[0-9]+)?
-----------------------------------------------------------------------------------------------------------------------
正则表达式版本
public static boolean isNumber(String s) { return s.matches("(\\s*)[+-]?((\\.[0-9]+)|([0-9]+(\\.[0-9]*)?))(e[+-]?[0-9]+)?(\\s*)"); }
相关文章推荐
- 优秀博文记录
- UVA 10480 Sabotage (最大流最小割)
- PHP利用超级全局变量$_GET来接收表单数据。
- SVM再次总结 - 1 - 准备知识
- NYOJ 42 一笔画问题
- Shell/Python实现Mysql读txt文本
- Android 获取存储路径
- 如何在 IIS 上搭建 mercurial server
- Android adb bugreport工具分析和使用
- 1065. A+B and C (64bit) (20)
- /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误解决
- Android adb bugreport工具分析和使用
- pip China
- 毕业生网站难点:上传表格,以及统计比率
- ODOO发邮件失败解决方法
- HTTPS详解SSL/TLS
- hd 1803 The MAX(sort)
- Tomcat 7.0.3x 启动慢并且遇到StackOverflowError的异常的解决办法
- 使用邮箱找回密码 springmvc
- TreeSet与TreeMap浅解