[LeetCode] 65. Valid Number 验证数字
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"
- Positive/negative sign - "+"/"-"
- Decimal point - "."
Of course, the context of these characters also matters in the input.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
这道题需要考虑的情况非常多,OJ的成功率很低,估计面试不会出此题的。
Java:
public boolean isNumber(String s) {
s = s.trim(); boolean pointSeen = false;
boolean eSeen = false;
boolean numberSeen = false;
boolean numberAfterE = true;
for(int i=0; i<s.length(); i++) {
if('0' <= s.charAt(i) && s.charAt(i) <= '9') {
numberSeen = true;
numberAfterE = true;
} else if(s.charAt(i) == '.') {
if(eSeen || pointSeen) {
return false;
}
pointSeen = true;
} else if(s.charAt(i) == 'e') {
if(eSeen || !numberSeen) {
return false;
}
numberAfterE = false;
eSeen = true;
} else if(s.charAt(i) == '-' || s.charAt(i) == '+') {
if(i != 0 && s.charAt(i-1) != 'e') {
return false;
}
} else {
return false;
}
} return numberSeen && numberAfterE;
}
Python:
class Solution:
# @param s, a string
# @return a boolean
# @finite automation
def isNumber(self, s):
INVALID=0; SPACE=1; SIGN=2; DIGIT=3; DOT=4; EXPONENT=5;
#0invalid,1space,2sign,3digit,4dot,5exponent,6num_inputs
transitionTable=[[-1, 0, 3, 1, 2, -1], #0 no input or just spaces
[-1, 8, -1, 1, 4, 5], #1 input is digits
[-1, -1, -1, 4, -1, -1], #2 no digits in front just Dot
[-1, -1, -1, 1, 2, -1], #3 sign
[-1, 8, -1, 4, -1, 5], #4 digits and dot in front
[-1, -1, 6, 7, -1, -1], #5 input 'e' or 'E'
[-1, -1, -1, 7, -1, -1], #6 after 'e' input sign
[-1, 8, -1, 7, -1, -1], #7 after 'e' input digits
[-1, 8, -1, -1, -1, -1]] #8 after valid input input space
state=0; i=0
while i<len(s):
inputtype = INVALID
if s[i]==' ': inputtype=SPACE
elif s[i]=='-' or s[i]=='+': inputtype=SIGN
elif s[i] in '0123456789': inputtype=DIGIT
elif s[i]=='.': inputtype=DOT
elif s[i]=='e' or s[i]=='E': inputtype=EXPONENT state=transitionTable[state][inputtype]
if state==-1: return False
else: i+=1
return state == 1 or state == 4 or state == 7 or state == 8
C++:
class Solution {
public:
bool isNumber(string s) {
bool num = false, numAfterE = true, dot = false, exp = false, sign = false;
int n = s.size();
for (int i = 0; i < n; ++i) {
if (s[i] == ' ') {
if (i < n - 1 && s[i + 1] != ' ' && (num || dot || exp || sign)) return false;
} else if (s[i] == '+' || s[i] == '-') {
if (i > 0 && s[i - 1] != 'e' && s[i - 1] != ' ') return false;
sign = true;
} else if (s[i] >= '0' && s[i] <= '9') {
num = true;
numAfterE = true;
} else if (s[i] == '.') {
if (dot || exp) return false;
dot = true;
} else if (s[i] == 'e') {
if (exp || !num) return false;
exp = true;
numAfterE = false;
} else return false;
}
return num && numAfterE;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 65. Valid Number 验证数字的更多相关文章
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [LintCode] Valid Number 验证数字
Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- Leetcode 65 Valid Number 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- [LeetCode] 65. Valid Number(多个标志位)
[思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- 瑞克和莫蒂|S3E03 Rick为什么要把自己变成一根腌黄瓜?
给尚未入坑Rick and Morty的小伙伴们为一点背景知识 S3E1在说什么? rick从监狱里回来,顺手搞垮了统治地球的银河联邦政府.Jerry因此丢了工作,非常不高兴.于是向Beth质问 ...
- CentOS7下的AIDE入侵检测配置
一.AIDE的概念 AIDE:Advanced Intrusion Detection Environment,是一款入侵检测工具,主要用途是检查文档的完整性.AIDE在本地构造了一个基准的数据库,一 ...
- DT6.0关于SQL注入漏洞修复问题
阿里云安全平台提示:Destoon SQL注入,关于: Destoon的/mobile/guestbook.php中$do->add($post);这行代码对参数$post未进行正确转义,导致黑 ...
- Sql操作时间
--. 当前系统日期.时间 -- ::27.277 --.时间操作 dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 dateadd(datepart,number,d ...
- [USACO08OCT]:打井Watering Hole(MST)
题意:有N个牧场,每个牧场修水井花费Wi,连接牧场花费Pij,问最小花费,使得每个牧场要么有水井,要么和有水井的牧场有通道. 思路:加一个格外的节点O,连接O表示修井,边权是修井的费用. 那么 ...
- Laravel —— 多模块开发
Laravel 框架比较庞大,更适用于比较大的项目. 为了整个项目文件结构清晰,不同部分分为不同模块很有必要. 一.安装扩展包 1.根据不同 Laravel 版本,选择扩展包版本. packagest ...
- django-导入应用包的搜索路径
创建应用包 在 settings.py注册和配置urls.py中要按顺序导入包名和应用名 settings.py INSTALLED_APPS = ( 'django.contrib.admin', ...
- 关于System.ArgumentNullException异常
什么是ArgumentNullException 当将 null 引用(Visual Basic 中为 Nothing)传递到不接受其作为有效参数的方法时引发的异常. 继承 Object Except ...
- jedis的连接池
1.需要先打开虚拟机,并开启Linux系统的端口号:6379: 其中,第一行代码为修改字符编码格式,解决SSH中文乱码问题. 2.开启redis: 3.利用连接池实现数据的存取: (1)代码实现: i ...
- Linux常用命令合集
常用命令合集 命令选项和参数 Linux中的命令格式为:command [options] [arguments] //中括号表示可选的,即有些命令不需要选项也不需要参数,但有的命令在运行时需要多个 ...