Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

解决方案:

public class Solution {
public boolean isNumber(String s) {
int i=0,n=s.length();
while(i<n&&Character.isWhitespace(s.charAt(i)))i++;
if(i<n&&(s.charAt(i)=='+'||s.charAt(i)=='-'))i++;
boolean isNumber=false;
while(i<n&&Character.isDigit(s.charAt(i)))
{
i++;
isNumber=true; //有数字就把 isNumber=true
}
if(i<n&&s.charAt(i)=='.')
{
i++;
while(i<n&&Character.isDigit(s.charAt(i))) //即出现'.', 则后面要么就没有了(i==n ,那么下面所有的判断都不满足了,),要么就是后面有数字,则isNumber=true )
{
i++;
isNumber=true;
}
}
if(isNumber&&i<n&&s.charAt(i)=='e') //前面没有数字,或者没有 '.' + 数字 这种形式(isNumber决定),那么当前即便为e,也不需要去处理
{
isNumber=false;
i++;
if(i<n&&(s.charAt(i)=='+'||s.charAt(i)=='-'))i++;
while(i<n&&Character.isDigit(s.charAt(i)))
{
i++;
isNumber=true;
}
}
while(i<n&&Character.isWhitespace(s.charAt(i)))i++;
return isNumber&&i==n;
}
}

注:e9 -> false ;  1. -> true ;  .1 -> true ;   49.e+9 -> true ;  .e9 -> false; .0e4 -> true 。

大致思路是从左到右,依次处理左边数字前的空格、+-号、‘.’、e(以及e后面的+-号)、数字、右边数字后的空格。‘.’ 以及e只会出现依次,所以用一次 if 即可,然后再判断它后面有没有数字有就

isNumber=true,再判断数字后面是不是还有空格。
特别说明: if(isNumber&&i<n&&s.charAt(i)=='e') 这句语句用的比较好,排除了 e 前面的多种情况,否则对于出现 e 这种情况来说,会有很多情况

 

Leetcode 详解(Valid Number)的更多相关文章

  1. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  2. Leetcode 详解(valid plindrome)

    Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...

  3. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  4. Leetcode详解Maximum Sum Subarray

    Question: Find the contiguous subarray within an array (containing at least one number) that has the ...

  5. Leetcode 详解(ReverseWords)

    Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...

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

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

  7. LeetCode OJ:Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  8. 74th LeetCode Weekly Contest Valid Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  9. Leetcode 详解(股票交易日)(动态规划DP)

    问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...

随机推荐

  1. linux中断处理原理分析

    Tasklet作为一种新机制,显然可以承担更多的优点.正好这时候SMP越来越火了,因此又在tasklet中加入了SMP机制,保证同种中断只能在一个cpu上执行.在软中断时代,显然没有这种考虑.因此同一 ...

  2. vpsmate安装

    安装需求 操作系统:CentOS/Redhat 5.4 或 5.4 以上版本,32位或64位均可,推荐使用 CentOS 6.2 64位. 内存大小:运行时占用约 20MB 左右的服务器内存. 请使用 ...

  3. 浏览器请求URL原理

    访问服务器url其实是访问DNS服务器,浏览器询问DNS服务器锁访问的url的IP是什么, 找到之后就会返回结果,找不到就继续去其他DNS中寻找,找到路径之后就会返回一个html文档,浏览器会解析这个 ...

  4. 深入理解 '0' "0" '\0' 0 之间的区别

    看来基础还是很重要的,基础不扎实就难以学好c语言,就别说写出高质量的c语言代码了.今天,我就被这个问题折磨的不行了,哈哈,不过现在终于明白了‘\0’ ,‘0’, “0” 之间的区别了.困惑和快乐与你分 ...

  5. Cheatsheet: 2016 11.01 ~ 11.30

    Web Getting Started With Vapor: A Swift Web Framework Front-end vs Back-end vs Network Performance S ...

  6. 关于win10连接不上ftp的解决方案

    win10系统连接ftp服务器的时候,会先出现假死,比如: 然后 就会报错: 面对这些问题:我们不需要关闭放火请,卸载杀毒软件等等无用的操作,只需要一步就能搞定: 把ftp:// 换成 file:\\ ...

  7. iOS10 拍照崩溃问题

    根据相对应得功能添加相关权限即可,没必要全部添加,后面的描述可以官方点,因为会以弹出框的形式访问的,比如相机权限后面的描述可以为:这个应用需要访问相机:后续如果发现其他iOS10上面的问题会及时更新的

  8. Babel 学习

    一,为了更明白地使用Babel, 先了解Babel 的发展过程. 现在Babel的版本是6, 相对于以前的版本, 它做了重大更新: 1, 模块化:所有的内部组件都变成了单独的包.打开Babel在Git ...

  9. ireport5.6+jasperreport6.3开发(五)--以javabean为基准的报表开发(action关联)

    这里的是定方法主要参照sturts2-jasperreport-plugin的完成方法(其实就是抄的) PDF的样子是这样的两页的pdf 然后action的配置是这样的(不要在意格式) @Parent ...

  10. java selenium后报错Element not found in the cache元素定位要重新赋值之前的定义

    习惯上把定位的元素在操作之前就定位好, 例如: WebElement element1=driver.findElement(...);      ----------declaration1 Web ...