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),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
随机推荐
- 2016 icpc-ec-final
一不小心惨变旅游队,不过上海的风景不错 顺带找其他队交流一下集训经验...或许可以成为选拔和集训16级的依据 A.直接模3就可以了,2^(3*n)%7=1 L.每场比赛3种情况,穷举就可以了 D.刚开 ...
- java动态代理原理
我们经常会用到Java的动态代理技术, 虽然会使用, 但是自己对其中的原理却不是很了解.比如代理对象是如何产生的, InvocationHandler的invoke方法是如何调用的?今天就来深究下Ja ...
- SQL Join的一些总结
1.1.1 摘要 Join是关系型数据库系统的重要操作之一,SQL Server中包含的常用Join:内联接.外联接和交叉联接等.如果我们想在两个或以上的表获取其中从一个表中的行与另一个表中的行匹配的 ...
- _beginthreadex注意事项
_beginthreadex 当失败时返回0 而不是 -1L _beginthreadex调用之后返回的HANDLE,必须手动CloseHandle,才能正确释放句柄.
- ChartControl 折线图 柱状图
添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...
- MySQL基本数据类型
MySQL数据类型包括:整型.浮点型.日期类型.字符型,这里用表格的方式详细说明每个数据类型,这些只要记住常用的即可,需要再查阅. 整型 数据类型 存储范围 字节 TINYINT 有符号值:-128 ...
- BLE编程中关键步骤
获取权限 <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permiss ...
- 绝对炫的3D幻灯片-SLICEBOX
绝对炫的3D幻灯片-SLICEBOX http://www.jq22.com/jquery-info31
- MVC文章汇总
http://www.cnblogs.com/yaozhenfa/category/541420.html http://www.cnblogs.com/yubaolee/p/3269043.html ...
- 使用Maven JGit-Flow Plugin
git flow 请参考 http://www.ituring.com.cn/article/56870 2.开始使用插件,在pom.xml中添加以下代码: https://bitbucket.org ...