65. Valid Number
题目:
Validate if a given string is numeric.
Some examples:"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
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.
链接: http://leetcode.com/problems/valid-number/
题解:
看到这种题目第一反应就是DFA了,不过怎么构建好的DFA真的很难。参考了leetcode讨论版。 Automata的知识还要好好学习学习,希望年底前还有时间。
首先对字符串进行trim,去除前后的space。之后构建DFA。输入有五种情况
- 0 - 9
- +, -
- e
- dot
- other
其中dot有种特殊情况, 就是 1.成立,但 .不成立,所以对有没有数字使用一个boolean变量来记录。 应该还可以再简化,要再研究研究。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public boolean isNumber(String s) {
if(s == null || s.length() == 0)
return false;
s = s.trim();
int state = 0;
boolean hasNum = false; for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) >= '0' && s.charAt(i) <= '9') {
hasNum = true;
if(state <= 2)
state = 2;
else
state = (state <= 5) ? 5 : 7;
} else if(s.charAt(i) == '+' || s.charAt(i) == '-') {
if(state == 0 || state == 3)
state++;
else
return false;
} else if (s.charAt(i) == '.') {
if(state <= 2)
state = 6;
else
return false;
} else if (s.charAt(i) == 'e') {
if(state == 2 || (hasNum && state == 6) || state == 7)
state = 3;
else
return false;
} else
return false;
} return (state == 2 || state == 5 || (hasNum && state == 6) || state == 7);
}
}
Test cases:
通过以下的例子我们可以看出,对dot我们需要额外判断,比如
"+.5e-5" - True
"+5." - True
"5e-10.6" - False 使用科学计数法以后不可以出现 dot
".5e10" - True
".e10" - False
"." - False
"+5.e10" - True
二刷:
还是用state machine,画图的方法。我们详细地分解一下每个步骤。
- 首先还是上面的图, 我们先对s进行trim操作,去除头尾的空格space
- 设置一个变量hasNum来判断在string中是否曾经出现过数字,这个对于判断state 6的dot很关键
- 从0开始遍历string,根据state machine写code,假设c为当前字符,我们考虑以下情况
- 当c为数字
- 当c为'+'或者'-'
- 当c为'.'
- 当c为'e', 这时要注意从s6到s3这条, 这里的条件为 state = s6 && hasNum,这样才可以进入s3
- 其他返回false
- 最后判断state是否在2, 5, 7以及 (state == 6 && hasNum)
Java:
Time Complexity - O(n),Space Complexity - O(1)
public class Solution {
public boolean isNumber(String s) {
if (s == null || s.length() == 0) {
return false;
}
s = s.trim();
int state = 0;
boolean hasNum = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
hasNum = true;
if (state <= 2) {
state = 2;
} else {
state = (state <= 5) ? 5 : 7;
}
} else if (c == '+' || c == '-') {
if (state == 0 || state == 3) {
state++;
} else {
return false;
}
} else if (c == '.') {
if (state <= 2) {
state = 6;
} else {
return false;
}
} else if (c == 'e') {
if (state == 2 || state == 7 || (state == 6 && hasNum)) {
state = 3;
} else {
return false;
}
} else {
return false;
}
}
return state == 2 || state == 5 || state == 7 || (state == 6 && hasNum);
}
}
三刷:
依然是画图使用state machine的方法。 上面的图有一个地方画错了, state 6的时候,不应该有一条自己连自己的链。需要找到一种更好的办法描述state 6的终止条件,和跳到state 3的条件。 state 6跳到state 3需要 hasNum + exp, 而终止时需要hasNum。
Java:
public class Solution {
public boolean isNumber(String s) {
if (s == null || s.length() == 0) return false;
s = s.trim();
int state = 0;
boolean hasNum = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
hasNum = true;
if (state <= 2) state = 2;
else if (state < 5) state = 5;
else if (state == 6) state = 7;
} else if (c == '.') {
if (state < 3) state = 6;
else return false;
} else if (c == 'e') {
if (state == 2 || (state == 6 && hasNum) || state == 7) state = 3;
else return false;
} else if (c == '+' || c == '-'){
if (state == 0 || state == 3) state++;
else return false;
} else {
return false;
}
} return state == 2 || state == 5 || state == 7 || (state == 6 && hasNum);
}
}
Reference:
http://postimg.org/image/n7lsslmgz
https://leetcode.com/discuss/13691/c-my-thought-with-dfa
https://leetcode.com/discuss/55915/lol-hard-to-understand-but-fast-8ms
https://leetcode.com/discuss/9013/a-simple-solution-in-cpp
https://leetcode.com/discuss/26682/clear-java-solution-with-ifs
https://leetcode.com/discuss/23447/a-clean-design-solution-by-using-design-pattern
https://leetcode.com/discuss/70510/a-simple-solution-in-python-based-on-dfa
https://leetcode.com/discuss/47396/ac-java-solution-with-clear-explanation
65. Valid Number的更多相关文章
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- 65. Valid Number 判断字符串是不是数字
[抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...
- 65. Valid Number *HARD*
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
随机推荐
- 【转】Mac 上 java 究竟在哪里,本文彻底让你搞清楚!
这篇文章可能比较适合那些在经常在Mac下进行Java编程开发,或者经常使用Java工具的朋友.不关心Java或者不了解Java的朋友可以绕过本文哈~ 1. Mac下当你在[终端]输入java -ver ...
- WPF 气泡尖角在左边、下面、右边、上面
由于项目需要,在弄一个气泡提示框,根据网上资料,使用Path可以将气泡画出来,下面是我画出来的. 1.气泡尖角在左边的: <Path Stroke="Black" Strok ...
- 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表中的标识列指定显式值
今天在处理数据时遇到这样一个错误 消息 8101,级别 16,状态 1,第 1 行 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.StockDetailValu ...
- MySQL ibdata1撑爆占满磁盘空间
MySQL主从由于ibdata1占满磁盘空间-->主从失效 因为设置了innodb_file_per_table = 1,ibdata1依旧撑爆占满磁盘空间 主从断的时候,IO线程在连接,SQL ...
- [CSS]学习总结
1. 遮挡层 .occlusion { opacity: -.35;/*透明程度*/ -moz-opacity: -.35; filter: alpha(opacity=-35); height: 1 ...
- 暂停更新Blog
今天非常不好意思的是老魏又要一次的暂停文章跟新了,原因是有些有问题老魏需要从新的梳理,加上这几天工作又开始忙碌起来了,所以这一阵子估计很难有有时间更新了. 不过老魏会抽一下时间更新文章的,不可能像2月 ...
- 为什么V8引擎这么快?(转载)
转载请注明出处:http://blog.csdn.net/horkychen Google研发的V8 JavaScript引擎性能优异.我们请熟悉内部程序实现的作者依源代码来看看V8是如何加速的. 作 ...
- 中国IT人,你们是否从没想过开发一款伟大的产品?
我也是今年刚毕业的,一毕业就做了猎头,从开始实习到正式工作,迄今为止接触的IT技术人不下上千人了.这里面有腾讯.阿里巴巴.百度.360.金山.金蝶.用友.华为.惠普等从事自主研发的大牛,也有很多软通. ...
- 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- [转载]Winform开发框架之统计图表的实现
在前面的一些随笔中,介绍了不少我的Winform框架的特性,上篇随笔<Winform开发框架之通用高级查询模块>对其中的通用高级模块进了一个整理说明,本篇继续介绍Winform开发框架重要 ...