Leetcode 详解(Valid Number)
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)的更多相关文章
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- Leetcode 详解(valid plindrome)
Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- Leetcode详解Maximum Sum Subarray
Question: Find the contiguous subarray within an array (containing at least one number) that has the ...
- Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- LeetCode OJ:Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 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 ...
- Leetcode 详解(股票交易日)(动态规划DP)
问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
随机推荐
- CSS3动画
参考:http://www.w3school.com.cn/css3/css3_animation.asp http://www.w3school.com.cn/tiy/t.asp?f=css3_im ...
- ListView 刷新加载控件
1.MaterialRefreshLayout刷新加载: 导入依赖: compile 'com.cjj.materialrefeshlayout:library:1.3.0' 布局 <com.c ...
- Python3基础 访问列表 小于指定索引值的所有元素
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Sublime Text 3 提高工作效率的使用技巧
Sublime Text 3对于Sublime Text 2压倒性的优势就是秒启动,启动非常非常快,所以从2012年到2016年我一直用Sublime Text 2,但是安装了3并且启动试用后,我再也 ...
- centos7 docker activemq
/ cd /home/activemq// wget http://apache.fayea.com/activemq/5.13.3/apache-activemq-5.13.3-bin.tar.gz ...
- CFNetwork的错误代码参考
所有的错误代码是在对“CFNetwork的错误代码参考” 小提取CFURL和CFURLConnection错误: kCFURLErrorUnknown = -998, kCFURLErrorCance ...
- Java准确地获取本地IP地址
问题 用Java获取本机IP地址,需要处理: 1. 多块网卡. 2. 排除loopback设备.虚拟网卡 看似简单的代码,写起来还是要小心一些的. 方案 HBase客户端获取本机IP的代码提供了一个很 ...
- thinkphp的学习笔记
# Thinkphp ## 什么是框架?(框架开发)> 框架就是通过提供一个开发Web程序的基本框架(比如提供了分页类.数据库操作类.文件操作类等),使用框架可以减少开发者代码的编写.> ...
- JS获取Url中传入的参数
一:后台获取,前台调用 后台: object value= Request.QueryString[key]; 前台js: $(function(){ var value="<%=va ...
- HTML meta锚点跳转 小tips
小tips meta锚点跳转 http://www.zhangxinxu.com/wordpress/2015/03/meta-http-equiv-refresh-content/