【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 ...
随机推荐
- 洛谷 P4568 [JLOI2011]飞行路线 解题报告
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ...
- 五、spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate
1.pom添加依赖 <!-- spring data jpa,会注入tomcat jdbc pool/hibernate等 --> <dependency> <group ...
- emoji表情存储到数据库的方法
方案1:修改数据库编码 为什么我们设置表的的字符类型为utf8却不能存放emoji呢?原来utf8可能是2或3或4个字节,而mysql的utf8是3个字节,存放一个emoji是需要4个字节的,自然不够 ...
- 神奇:java中float,double,int的值比较运算
float x = 302.01f; System.out.println(x == 302.01); //false System.out.println(x == 302.01f); // ...
- [机器学习]-SVD奇异值分解的基本原理和运用
SVD奇异值分解: SVD是一种可靠的正交矩阵分解法.可以把A矩阵分解成U,∑,VT三个矩阵相乘的形式.(Svd(A)=[U*∑*VT],A不必是方阵,U,VT必定是正交阵,S是对角阵<以奇异值 ...
- jdk1.8.0_45源码解读——Map接口和AbstractMap抽象类的实现
jdk1.8.0_45源码解读——Map接口和AbstractMap抽象类的实现 一. Map架构 如上图:(01) Map 是映射接口,Map中存储的内容是键值对(key-value).(02) A ...
- Django Book学习笔记(下)
Django的Session框架 对于Django加密,大致使用这样的格式: hashtype$salt$hash 原因? 一次哈希是一次单向的加密过程,你能容易地计算出一个给定值的哈希码,但是几乎不 ...
- iOS编码规范参考
目录 注释 1.1 多行注释 1.2 单行注释 1.3 函数的注释 命名 2.1 常量的命名 2.2 函数的命名 2.3 变量的命名 2.3.1 成员变量 2.3.2 公 ...
- 线性筛的同时得到欧拉函数 (KuangBin板子)
线性筛的思想:每个被筛的数是通过它最小的质因子所筛去的. 这种思想保证了每个数只会被筛一次,从而达到线性.并且,这个思想实现起来非常巧妙(见代码注释)! 因为线性筛的操作中用到了倍数的关系去实现,因此 ...
- PyQT5 No module named ‘PyQt5.QtWebEngineWidgets’
PyQT5查找不到模块QtWebEngineWidgets pip install pyqt5==5.10.1 或 安装64位的Pyhon解释器