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),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
随机推荐
- Android.mk 文件语法详解
0. Android.mk简介: Android.mk文件用来告知NDK Build 系统关于Source的信息. Android.mk将是GNU Makefile的一部分,且将被Build Syst ...
- Install Ansible on Mac OSX
from: https://devopsu.com/guides/ansible-mac-osx.html and : https://devopsu.com/guides/ansible-post- ...
- freemaker时间格式转换,精确到毫秒
在开发中,需要将时间以 2016-09-20 12:00:01:723 的形式里用freemaker展示在页面上,找了好久,终于找到了答案. "createTime":" ...
- test homework2 ~ faulty program
Program1:
- Java学习路线图,专为新手定制的Java学习计划建议
怎么学习Java,这是很多新手经常会问我的问题,现在我简单描述下一个Java初学者到就业要学到的一些东西: 首先要明白Java体系设计到得三个方面:J2SE,J2EE,J2ME(KJAVA).J ...
- MySQL存储IP地址操作
数据库数据表创建语法: DROP TABLE IF EXISTS `admin`; CREATE TABLE IF NOT EXISTS `admin`( `adminid` INT UNSIGNED ...
- Intellij 打开就闪退或关闭
找到安装目录的bin目录,搜索vmoptions可以看到两个文件, idea.exe.vmoptions idea64.exe.vmoptions 这两个文件就是IDEA的一些配置文件,带64位 ...
- mysql忘记密码
修改 /etc/my.cnf 添加 skip-grant-tables 修改完后重启mysql 这样就可以直接进入mysql 然后修改mysql数据库密码 mysql>update user s ...
- Evolutionary Computing: Assignments
Assignment 1: TSP Travel Salesman Problem Assignment 2: TTP Travel Thief Problem The goal is to find ...
- ios中的http:get,post,同步,异步
一.服务端 1.主要结构: