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 解题报告的更多相关文章

  1. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  2. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  3. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  4. LeetCode: Valid Sudoku 解题报告

    Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...

  5. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

随机推荐

  1. MVC时间对比及时间范围判断

    方法一:使用DateTime.Compare 方法 public static int Compare( DateTime t1, DateTime t2 ) t1 早于 t2:小于零t1 与 t2  ...

  2. JProfiler 解决 Java 服务器的性能跟踪

    作者:徐建祥(netpirate@gmail.com) 时间: 2006/01/05 来自:http://www.anymobile.org 1.摘要......................... ...

  3. Linux(CentOS)的server安装及配置图解(图文)

    CentOS,据说这个如今在server上装机量非常大. 安装的分区划分 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdXBpMnU=/font/5a6L ...

  4. 【laravel5.4】 Composer移除依赖

    1.在一次使用composer安装依赖的时候,安装错了包,在其中文网站却找不到移除依赖的命令,只好使用按照官网说法: 为了从命令行获得帮助信息,请运行 composer 或者 composer lis ...

  5. linux下使用speedtest测速

    Speedtest测试网络上传/下载速度还是挺不错的,windows下非常方便.Linux下也可以很方便的使用命令行speedtest来测试.speedtest是一个用Python编写的轻量级Linu ...

  6. pip安装注意事项

    pip源 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...

  7. iOS 9 适配中出现的坑

    整理 iOS 9 适配中出现的坑(图文) 2015-10-22 iOS开发 库克表示:“现在在中国有150多万的开发者在iOS当中开发应用程序,我们鼓励更多的人开发应用程序,也鼓励更多的创业加入.” ...

  8. 房产地图google map的初步应用点滴.2)(转)

    房产地图google map的初步应用点滴.1)房产地图google map的初步应用点滴.2)房产地图google map的初步应用点滴.3) 房产地图google map的初步应用点滴.4) 本来 ...

  9. Chart.js 学习笔记

    1.引入Chart.js 文件 <script src="Chart.js"></script> 2.在html中创建画布 <canvas id=&q ...

  10. 【jQuery】JQuery-ui autocomplete与strtus2结合使用

    汉字搜索效果图: 拼音首字母搜索效果图:  1)数据库表及函数(SQL Server 2008) 先来建立数据库表City,它包含两个字段CityID,CityName. CREATE TABLE C ...