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 " => ...
随机推荐
- Java生成html静态网页
public static void makeHtml() { try { URL url = new URL("http://www.baidu.com/"); //本实事例通过 ...
- Fortify
sourceanalyzer -b my_buildid -scan -f xxx.fpr -b 取一个build的ID号,通常以这个项目名称加扫描时间为buildID-Xmx 指定JVM使用的最大 ...
- ASP.NET MVC Global.cs - 应用程序事件
一.Application_End Application_End:网站关闭,或重启时,会触发该方法 二.Application_Start 第一个访问网站的用户会触发该方法. 通常会在该 ...
- javascript DOM操作之 querySelector,querySelectorAll
javascript DOM操作之 querySelector,querySelectorAll
- SAMBA 共享服务器搭建
yum install samba service smb start chkconfig smb on 1.给要共享的文件夹赋权限 777 2.修改 smb 的配置文件:/etc/samba/smb ...
- jquery 隐藏表单元素
1.html <label for="lbl" >电压等级:</label> <input class="easyui-combobox&q ...
- 【bzoj4034】[HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都 ...
- Hadoop之Storm命令
Hadoop之Storm命令 1.storm核心概念 stream--->一列火车 tuple--->一节车厢 数据--->乘客 spout--->始发站 bolt---> ...
- cocos2d-x多分辨率适配方案:setDesignResolutionSize使用
1. setDesignResolutionSize使用方法及主要的三种适配模式 在cocos2d-x 2.0里,提供了一个叫做setDesignResolutionSize的方法,直接一次设置就可以 ...
- 在lua脚本中使用我们自定义的精灵类
首先创建cocos2dx-lua项目,然后在项目中添加我们的自定义精灵类:这里Himi类名为:HSprite // // HSprite.h // cocos2dx_lua_tests_by_Himi ...