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),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
随机推荐
- jQuery事件总结
blur() 触发或绑定blur事件.$("input").blur(function(){ $("input").css("background-c ...
- Word 宏命令大全
1. 为宏命令指定快捷键.在WORD中,操作可以通过菜单项或工具栏按钮实现,如果功能项有对应的快捷键的话,利用快捷键可以快速实现我们需要的功能.如最常见的CTRL+O.CTRL+A等等.WOR ...
- 百度-official
1.请描述html5新增的一些标签,描述这些标签的用法和语义 2.css属性position的属性值有哪些,描述它们的作用 3.常见的浏览器端的存储技术有哪些,以及它们的优缺点 4.程序定义如下: v ...
- 如何为CriteriaOperator过滤对象转换为lambda表达式,即:linq to xpo的动态where语句
How to convert the CriteriaOperator to a lambda expression, so, the latter expression can be used in ...
- OneSQL助力永辉超市大卖特卖
数据库集群查询达到10w/s,更新操作5k/s,正常! 应用并发连接达到历史高峰4倍,正常! 业务平稳运行,正常! 永辉微店527大促,圆满成功!这标志着平民软件数据库工程师.accenture咨询实 ...
- Python之路 day2 集合的基本操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #集合是无序的 集合的关系测试, 增加,删除,查找等操作 ''' #列表去重 ...
- 移动端html模版
<!DOCTYPE html><html><head> <title>时钟</title> <meta charset="u ...
- ajax中使用post传值数组array
如果我们在data中想放入array的参数,根据在网上搜索的结果有如下方法,但是没有一个成功的……我还是贴出来,别人成功了,也许是我哪里不对,日后也许还有机会研究..在文章的最后贴出了转化为json的 ...
- python的字符串内建函数
http://www.ziqiangxuetang.com/python/python-strings.html
- javascript中运算符的优先级
运算符优先级 JavaScript中的运算符优先级是一套规则.该规则在计算表达式时控制运算符执行的顺序.具有较高优先级的运算符先于较低优先级的运算符执行.例如,乘法的执行先于加法. 下表按从最高到最低 ...