【LeetCode】65. Valid Number
Difficulty: Hard
More:【目录】LeetCode Java实现
Description
Validate if a given string can be interpreted as a decimal number.
Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true" -90e3 " => true" 1e" => false"e3" => false" 6e-1" => true" 99e2.5 " => false"53.5e93" => true" --6 " => false"-+3" => false"95a54e53" => false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
- Numbers 0-9
- Exponent - "e" or "E"
- Positive/negative sign - "+"/"-"
- Decimal point - "."
Of course, the context of these characters also matters in the input.
Intuition
Method1: A valid number is in the form of A.B e/E A (A: integer, B: unsigned integer), so it is helpful to break the problem down to several components that can be solved individually. Detailed solution refer to: 表示数值的字符串
Method2: Use some flags(eSeen, pointSeen, isNum) while scan each character in the String. The solution is shown below.
Solution
public boolean isNumber(String s) {
if(s==null || s.length()<=0)
return false;
s=s.trim();
boolean isNum=false;
boolean pointSeen=false;
boolean eSeen=false;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='+'||s.charAt(i)=='-'){
if(i!=0 && s.charAt(i-1)!='e' && s.charAt(i-1)!='E')
return false;
}else if(Character.isDigit(s.charAt(i))){
isNum=true;
}else if(s.charAt(i)=='.'){
if(eSeen || pointSeen)
return false;
pointSeen=true;
}else if(s.charAt(i)=='e' || s.charAt(i)=='E' ){
if(eSeen || !isNum)
return false;
eSeen=true;
isNum=false;
}else
return false;
}
return isNum;
}
Complexity
Time complexity : O(n)
Space complexity : O(1)
What I've learned
1. Ought to make the best of flags. Learn to use flags well.
More:【目录】LeetCode Java实现
【LeetCode】65. Valid Number的更多相关文章
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
随机推荐
- 【BZOJ5138】[Usaco2017 Dec]Push a Box(强连通分量)
[BZOJ5138][Usaco2017 Dec]Push a Box(强连通分量) 题面 BZOJ 洛谷 题解 这题是今天看到萝卜在做然后他一眼秒了,我太菜了不会做,所以就来做做. 首先看完题目,是 ...
- dp乱写2:论dp在不在dp中(但在dp范畴)内的应用
最近正儿八经的学习了dp,有一些题目非常明显看出来就是dp了比如说:过河卒.方格取数.导弹拦截.加分二叉树.炮兵阵地更加明显的还有:采药.装箱问题.过河.金明的预算方案.今天来谈谈dp的dp在不在dp ...
- 解题:九省联考2018 IIIDX
题面 我当时在考场上划水的时候好像乱搞搞了20pts,然后发现一堆同届的都写了55pts的贪心=.=??? 那就先说那55pts的贪心吧,这个现在看起来还是非常显然的,就是按题意来每一块是分属一个点的 ...
- JavaScript--序列化以及转义
一.序列化 1.1 序列化 将其他对象转换为字符串,用法:JSON.stringify() var li = [1,2,3,4]; var new_li = JSON.stringify(li); n ...
- INF文件详解
安装信息(Setup Information)文件是Windows系统支持的一种安装信息存放文件,一般以INF作为扩展名,因此也叫INF文件.安装信息INF文件与Windows内建的安装服务引擎(AP ...
- JIRA项目管理搭建
部署JIRA 7.2.2 for Linux 转自:http://www.yfshare.vip/2017/05/09/%E9%83%A8%E7%BD%B2JIRA-7-2-2-for-Linux/ ...
- BZOJ1009 GT考试
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数X1X2.. ...
- xmlhttprequest upload
html5 带进度上传 function fileSelected() { var file = document.getElementById('fileToUpload').files[0]; i ...
- websocket知识简单总结!
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Number of Airplanes in the Sky
Given an interval list which are flying and landing time of the flight. How many airplanes are on th ...