Valid Number
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.
分析:http://blog.csdn.net/linhuanmars/article/details/23809661
这是一道检查字符串输入是否为合法的题目。基本规则是按照科学计数法,所以会出现的特殊字符有以下几个:符号位‘+’,‘-’,小数点‘.’,还有‘e’和‘E’,剩下的就只有数字0-9了,其他字符如果出现就是非法字符,返回false。数字字符在哪里出现都是ok的,我们主要考虑几个特殊字符的情况。
对于小数点出现的时候,我们要满足一下这些条件:(1)前面不能有小数点或者‘e’和‘E’;(2)前一位是数字(不能是第一位)或者后一位要是数字(不能是最后一位)。
对于正负号出现的情况,要满足条件:(1)必须是第一位或者在‘e’和‘E’后一位;(2)后一位要是数字。
对于‘e’和‘E’的情况,要满足:(1)前面不能有‘e’和‘E’出现过;(2)不能是第一位(前面没数字科学计数没有意义)或者最后一位(后面没数字就不用写指数了)。
根据上面列举的情况,我们用两个标签和做前后位的判断来实现,算法复杂度比较明显是O(n)的,只需要O(1)的额外空间。代码如下:
代码:http://ideone.com/7MIXt6
public class Solution {
static boolean isNumber(String s) {
if (s == null || s.trim().length() == ) return false;
s = s.trim();
int n = s.length(), opCount = ;
boolean hasE = false, hasNum = false, hasPoint = false;
// Go through the characters
for (int i = ; i < n; i++) {
char ch = s.charAt(i);
// input value
if (!(ch <= '' && ch >= '' || ch == '.' || ch == 'e' || ch == 'E' || ch == '+' || ch == '-'))
return false;
// number
if (ch >= '' && ch <= '')
hasNum = true;
// Case e/E
// (1) 前面不能有‘e’和‘E’出现过;(2)前面不能没数字或者最后一位(后面没数字就不用写指数了)。
if (ch == 'e' || ch == 'E') {
if (hasE || !hasNum || i == n - ) return false;
hasE = true;
}
// Case decimal point
// (1) 前面不能有小数点或者‘e’和‘E’;(2)单独的decimal point 不是valid数字。
if (ch == '.') {
if (hasPoint || hasE || i == n - && !hasNum) return false;
hasPoint = true;
}
// Case sign
// (1)必须是第一位或者在‘e’和‘E’后一位;(2)后一位要是数字
if (ch == '+' || ch == '-') {
if (opCount == || i == n - ) return false;
if (i > && !(s.charAt(i - ) == 'E' || s.charAt(i - ) == 'e')) return false;
opCount++;
}
}
return true;
}
}
Valid Number的更多相关文章
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [LintCode] Valid Number 验证数字
Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...
- Valid Number @python
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 配置域名服务器报错named[822]: dns_rdata_fromtext /etc/bind/db.asertest.com mail not a valid number
问题描述: 为了配置邮件服务器,更改了相关域名,改完后,重启bind9报错 Mar 17 14:39:39 DnsServer2 named[822]: dns_rdata_fromtext: /et ...
- [Swift]LeetCode65. 有效数字 | Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
随机推荐
- Mysql 常用函数
统计函数: count() 统计记录条数,如 select count(*) from stu; sum() 统计记录字段的和,如select sum(salary) from emp; ...
- java中跳出if判断
今天学到的一点儿新东西一个if判断里面有好多东西,紧接着还有其他代码,不能使用return来结束这个if判断这时候,就需要这样: out:if (!"null".equals(re ...
- JQ nextALL() 实现遍历
在我们的 手机端 常常需要 应用到 hide 和 show 的方法 来节省页面的版块 那么我们需要更多的是 需要 show 一个 <li> hide 剩下的 <li> 那么 ...
- What is the most common software of data mining? (整理中)
What is the most common software of data mining? 1 Orange? 2 Weka? 3 Apache mahout? 4 Rapidminer? 5 ...
- Git命令家底儿及Git数据通信原理详解
http://geek.csdn.net/news/detail/72807?utm_source=tuicool&utm_medium=referral
- 有关html5设计那些事,你真的考虑过前端的实现吗(最近别人经常问我这种问题,所以我就写一篇了,可能也有别人和我一样吐槽过)
很久以前在安卓2.0系统刚刚的时候就对HTML5比较关注!因为我也是那个时候刚刚入行做前端的.那个时候最大的乐趣就是看着w3plus上面各种css3的效果,觉得哇,好牛逼原来可以这样做,然后3年过去了 ...
- Form表单中method为get和post的区别
序,form表单中的方法分为get和post,但你都知道他们之间的区别吗? Form表单中method为get和post的区别: 例子如下,有个Form表单. <form action=&quo ...
- PHP中的Memcache详解
一.Memcache简介 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力.它可以应 ...
- handsontable学习
http://blog.csdn.net/mafan121/article/category/3273599
- boss设计参考的脑图