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. curl命令(测试连接命令)

    curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括HTTP.HTTPS.f ...

  2. java 泛型 精析

      Created by Marydon on 1.概述 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数: 这种参数类型可以用在类.接口和方法的 ...

  3. QQ概念版(WPF制作)

    984 QQ概念版 编辑   QQ 概念版是腾讯首款NUI(自然用户交互)产品,全面实现了多点触摸操作.是腾讯利用微软最新一代的客户端展现层技术--WPF,打造的IM产品. 中文名 QQ 概念版 游戏 ...

  4. Spring MVC Beginner’s Guide勘误表

    - 17 submitted: last submission 09 Dec 2016 Page number: 213 Qauntity should be: Quantity Page numbe ...

  5. 一些很经典的JavaScript的问题

    1.作用域 (function() { var a = b = 5; })(); console.log(b); 输出:5 陷阱是,在函数表达式中有两个赋值,但a是用关键字var 来声明的,这意味着a ...

  6. Visual C#两分钟搭建BHO IE钩子

    微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对IE进行二次开发和操作. 在通过编写BHO程序数月后, 我希望把我的一些经验告诉才开始的同志, 避 ...

  7. jquery 事件小事例

    用户名变灰 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...

  8. C#将Access数据库导出为JSON

    一个Access数据库包含若干首诗歌,每首诗有content.author.title.description四个字段 using System; using System.Data; using S ...

  9. 马老师 LNMP生产环境Web架构 笔记

    http协议和缓存原理.多路IO模型: MIME机制,Multipurpose Internet Mail Extensions,多用户互联网邮件扩展.MIME使用一个简单的字符串组成,最初是为了标识 ...

  10. PO_标准内部请购内部采购单抛转订单模组(流程)

    2014-06-03 Created By BaoXinjian