LeetCode: Valid Number 解题报告
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.
SOLUTION 1:
我们设置3个FLAG:
1. num
2. exp
3. dot
有以下情况:
(1). 出现了e,则前面要有digit,不能有e. 并且后面要有digit.
(2). 出现了. 那么是一个小数,那么前面不可以有.和e
(3). 出现了+, - 那么它必须是第一个,或者前一个是e,比如" 005047e+6"
实际的代码实现如下,相当的简洁。
感谢答案的提供者:http://www.ninechapter.com/solutions/valid-number/
大神哇哇哇!
public class Solution {
public boolean isNumber(String s) {
if (s == null) {
return false;
}
// cut the leading spaces and tail spaces.
String sCut = s.trim();
/*
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
*/
int len = sCut.length();
boolean num = false;
boolean exp = false;
boolean dot = false;
for (int i = 0; i < len; i++) {
char c = sCut.charAt(i);
if (c == 'e') {
if (!num || exp) {
return false;
}
exp = true;
num = false; // Should be: 2e2 , so there should be number follow "e"
} else if (c <= '9' && c >= '0') {
num = true;
} else if (c == '.') {
if (exp || dot) { // can't be: e0.2 can't be: ..
return false;
}
dot = true;
} else if (c == '+' || c == '-') {
if (i != 0 && sCut.charAt(i - 1) != 'e') { // filter : " 005047e+6", this is true.
return false;
}
} else {
// invalid character.
return false;
}
}
return num;
}
}
请至主页君的GitHUB: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IsNumber.java
用正则其实也可以哦:
推荐一些较好的教程:
http://developer.51cto.com/art/200912/166310.htm
http://luolei.org/2013/09/regula-expression-simple-tutorial/
http://net.tutsplus.com/tutorials/php/regular-expressions-for-dummies-screencast-series/
http://deerchao.net/tutorials/regex/regex.htm
主页君就不写了,因为觉得正则实在是写不出来在面试时,真的太复杂了。
给个大神写好的正则的解答:
http://blog.csdn.net/fightforyourdream/article/details/12900751?reload
LeetCode: Valid Number 解题报告的更多相关文章
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- LeetCode: Valid Sudoku 解题报告
Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
随机推荐
- 减小App包的大小
检查.ipa文件 首先获得app的ipa文件. 将ipa文件的后缀改为.zip,解压得到包内容. 查看资源文件哪个最大.然后试着对最大的文件即可处理 图片 尽量使用8-bit图片 使用8-bit的PN ...
- elasticsearch5.4体验
Elasticsearch是基于Lucene分布式.实时查询的搜索分析引擎.我目前应用在日志集中化处理上... 01.下载elasticsearch5.4 https://www.elasti ...
- spring MVC之构造ModelAndView对象
spring MVC之构造ModelAndView对象 ---------- 构造ModelAndView对象 当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndV ...
- C-I/O操作函数详解
EOF: End Of File, 文字流结尾, 这里的文字流可以是文件(file), 也可以是标准输入(stdin), 它的值在任何可能出现的字符之外(-1) 先列出三种基本类型操作函数 这里面返回 ...
- HDUOJ---kiki's game
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others)Total ...
- JavaScript:用JS实现加载页面前弹出模态框
用JS实现加载页面前弹出模态框 主要的JavaScript 代码是: <script> //加载模态框 $('#myModal').modal(); $(document).ready(f ...
- ADO,OLEDB,ODBC,DAO,RDO的区别说明
http://www.jb51.net/article/31286.htm http://wenku.baidu.com/link?url=D7iis1tmkyXTCqjsezsvYtYHWG8jK- ...
- UltraEdit打开中文乱码
http://hellofs.blog.51cto.com/6109153/1187548 UltraEdit很强大,但是今天发现在打开含有中文的文本时会乱码,还是在配置里找找相关的设置,可以在如图的 ...
- SQL Server因为数据库正在使用,所以无法获得对数据库的独占访问权
恢复数据库: 恢复数据库之前,先执行下面这句话 ALTER DATABASE [mpn_stat] SET OFFLINE WITH ROLLBACK IMMEDIATE 执行恢复数据库SQL语句 R ...
- 数据源与JNDI资源实现JSP数据库连接池实例
名词解释:JNDI的全称是java命名与目录接口(Java Naming and Directory Interface),是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通 ...