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 " => ...
随机推荐
- NoSQL 简介及什么是AICD
NoSQL 简介 NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关 ...
- linux 下 zip unzip压缩与解压
注:*压缩成限.zip格式文件 常用解压缩: [root@mysql test]# unzip -o test.zip -d tmp/ 将压缩文件test.zip在指定目录tmp下解压缩,如果已有相同 ...
- 自定义列表dl的使用原因和场合
为什么要使用自定义列表? dl和ol, ul的区别? 要正确理解dl的意图, 理解 dl的 "语义" ! 才能知道为什么要使用dl, 以及在什么时候/ 什么情况下使用 dl? dl ...
- oracle 中的trunc()函数及加一个月,一天,一小时,一分钟,一秒钟方法
返回处理后的数据,不同于round()(对数值进行四舍五入处理),该函数不对指定小数前或后的数值部分进行舍入处理. 语法:trunc(number[,decimals]) 其中,number为待做处理 ...
- golang笔记——IDE
可选方案有 Lite IDE\GoSublime\Visual Studio Code\Goclipse\Vim 1.Lite IDE 这是国人开发的开源且跨平台的 golang 专属IDE,也算是目 ...
- VTK初学一,a_Vertex图形点的绘制
系统:Win8.1 QT版本:2.4.2,Mingw VTK版本:6.3 2. main.cpp #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #incl ...
- 如何在发布博客时插入复杂公式——Open Live Writer
1.http://latex.codecogs.com/eqneditor/editor.php 2.使用Word发布
- 解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法
Linux下默认cp命令是有别名的(alias cp='cp -i'),无法在复制时强制覆盖,即使你用 -f 参数也无法强制覆盖文件,下面提供几个从网上找的Linux下cp命令覆盖的方法. 1)取消c ...
- JAVA 笔记
一.Java基础以及面向对象编程1.float类型的数自动转换成double类型时,可能会出现前后不相等的情况,因为有些数不能够用有限的二进制位精确表示.2.右移>>右移,左边空出位以符号 ...
- objective-c-sort-keys-of-nsdictionary-based-on-dictionary-entries
NSArray *keys = [someDictionary allKeys]; NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NS ...