Difficulty: Hard

 More:【目录】LeetCode Java实现

Description

Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

  • Numbers 0-9
  • Exponent - "e" or "E"
  • Positive/negative sign - "+"/"-"
  • Decimal point - "."

Of course, the context of these characters also matters in the input.

Intuition

Method1: A valid number is in the form of A.B e/E A (A: integer, B: unsigned integer), so it is helpful to break the problem down to several components that can be solved individually. Detailed solution refer to: 表示数值的字符串

 Method2: Use some flags(eSeen, pointSeen, isNum) while scan each character in the String. The solution is shown below.

Solution

    public boolean isNumber(String s) {
if(s==null || s.length()<=0)
return false;
s=s.trim();
boolean isNum=false;
boolean pointSeen=false;
boolean eSeen=false;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='+'||s.charAt(i)=='-'){
if(i!=0 && s.charAt(i-1)!='e' && s.charAt(i-1)!='E')
return false;
}else if(Character.isDigit(s.charAt(i))){
isNum=true;
}else if(s.charAt(i)=='.'){
if(eSeen || pointSeen)
return false;
pointSeen=true;
}else if(s.charAt(i)=='e' || s.charAt(i)=='E' ){
if(eSeen || !isNum)
return false;
eSeen=true;
isNum=false;
}else
return false;
}
return isNum;
}

  

Complexity

Time complexity : O(n)

Space complexity :  O(1)

What I've learned

1. Ought to make the best of flags. Learn to use flags well.

 More:【目录】LeetCode Java实现

【LeetCode】65. Valid Number的更多相关文章

  1. 【一天一道LeetCode】#65. Valid Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...

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

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

  3. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

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

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

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

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

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  8. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  9. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

随机推荐

  1. 简单prufer应用

    [bzoj1005] Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? ...

  2. HGOI20181031 模拟题解

    sol:第一题就DP?!然后写了O(n^2) dp再考虑优化!!!(尽量部分分带上!!!) 我写了正确的dp然后优化错了,具体的dp方法是考虑到对于右侧到左侧他是没有后效性的 所以定义f[i]为i及以 ...

  3. mes平台的一些方法

    1.打开的一个缓存的页面的代码 $.openPane({ "width":"1500px",     "height":"1000 ...

  4. Centos 搭建 http服务器

    1,安装 yum install httpd 2,查看是否安装成功 netstat [root@localhost ~]# netstat -anp | grep 80      tcp 0 0 :: ...

  5. linux command ------ netstat

    netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接口设备的状态信息. 语法选项 netstat [选项] -a或--all:显示所有连线中的 ...

  6. linux sed文本

    sed介绍 sed(stream editor)是一种非交互式的流编辑器,通过多种转换修改流经它的文本.默认情况下,sed不会改变原文件本身,而只是对流经sed命令的文本进行修改,并将修改后的结果打印 ...

  7. Java基础-原码反码补码

    Java基础-原码反码补码 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 注意,我们这里举列的原码和反码只是为了求负数的补码,在计算机中没有原码,反码的存在,只有补码. 一.原码 ...

  8. Java FileReader使用相对路径读取文件

    Java FileReader使用相对路径读取文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 在进行编程时需要时常更换主机进行测试,如果使用绝对路径则需要经常更改,为此使用相对路径是一个 ...

  9. Solr记录-solr文档xml

    Solr添加文档(XML) 在上一章中,我们学习解释了如何向Solr中添加JSON和.CSV文件格式的数据.在本章中,将演示如何使用XML文档格式在Apache Solr索引中添加数据. 示例数据 假 ...

  10. JD m站自我解析理解

    第一步:从首页着手 文档部分:应用的是H5默认文档开头 即:<!DOCUMENT html> head部分:放了一些相关的JS,title描述,然后就是meta表述了.比较有参考的如下 & ...