LeetCode: Valid Number 解题报告
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.
SOLUTION 1:
我们设置3个FLAG:
1. num
2. exp
3. dot
有以下情况:
(1). 出现了e,则前面要有digit,不能有e. 并且后面要有digit.
(2). 出现了. 那么是一个小数,那么前面不可以有.和e
(3). 出现了+, - 那么它必须是第一个,或者前一个是e,比如" 005047e+6"
实际的代码实现如下,相当的简洁。
感谢答案的提供者:http://www.ninechapter.com/solutions/valid-number/
大神哇哇哇!
public class Solution {
public boolean isNumber(String s) {
if (s == null) {
return false;
}
// cut the leading spaces and tail spaces.
String sCut = s.trim();
/*
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
*/
int len = sCut.length();
boolean num = false;
boolean exp = false;
boolean dot = false;
for (int i = 0; i < len; i++) {
char c = sCut.charAt(i);
if (c == 'e') {
if (!num || exp) {
return false;
}
exp = true;
num = false; // Should be: 2e2 , so there should be number follow "e"
} else if (c <= '9' && c >= '0') {
num = true;
} else if (c == '.') {
if (exp || dot) { // can't be: e0.2 can't be: ..
return false;
}
dot = true;
} else if (c == '+' || c == '-') {
if (i != 0 && sCut.charAt(i - 1) != 'e') { // filter : " 005047e+6", this is true.
return false;
}
} else {
// invalid character.
return false;
}
}
return num;
}
}
请至主页君的GitHUB: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IsNumber.java
用正则其实也可以哦:
推荐一些较好的教程:
http://developer.51cto.com/art/200912/166310.htm
http://luolei.org/2013/09/regula-expression-simple-tutorial/
http://net.tutsplus.com/tutorials/php/regular-expressions-for-dummies-screencast-series/
http://deerchao.net/tutorials/regex/regex.htm
主页君就不写了,因为觉得正则实在是写不出来在面试时,真的太复杂了。
给个大神写好的正则的解答:
http://blog.csdn.net/fightforyourdream/article/details/12900751?reload
LeetCode: Valid Number 解题报告的更多相关文章
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- LeetCode: Valid Sudoku 解题报告
Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【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: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
随机推荐
- python easy_install 发生Unable to find vcvarsall.bat错误的处理方法
用python安装mmseg分词包时发生了 Unable to find vcvarsall.bat 错误 Searching for mmseg Reading http://pypi.python ...
- [C#]记录程序耗时的方法【转发】
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // H ...
- CentOS 查看系统版本号
查看centos版本: cat /etc/issuecat /etc/redhat-release 查看系统位数: getconf LONG_BIT 查看内核版本: uname -r cat /pro ...
- 转:基于 linux 平台的 libpcap 源代码分析
libpcap 是 unix/linux 平台下的网络数据包捕获函数包,大多数网络监控软件都以它为基础.Libpcap 可以在绝大多数类 unix 平台下工作,本文分析了 libpcap 在 linu ...
- 列表生成式&生成器表达式
一.列表生成式 def func(): return [lambda x:i*x for i in range(4)] print([j(2) for j in func()]) 结果:[6,6,6, ...
- Megcup 2017 决赛第一题 规则
2017Megcup 2017Megcup决赛第三题题解 只做出了一道题,虽然慢慢地退出了前128名,但还是要记录一下. 10点钟开始,一看第一题很熟悉,因为研究过格点图中电流问题,其实就是求解线性方 ...
- C#中的Invoke和BeginInvoke
一.Control#Invoke() 和Control#BeginInvoke() 在非UI线程中调用MessageBox.Show()结果是非模态对话框: 在UI线程中调用MessageBox.Sh ...
- Mac下搭建svn服务器和XCode配置svn
先打开命令行终端. 1.创建svn repository svnadmin create /yourpath/svnroot/repository 2.配置svn用户权限. / yourpath /s ...
- python学习笔记——多进程中共享内存Value & Array
1 共享内存 基本特点: (1)共享内存是一种最为高效的进程间通信方式,进程可以直接读写内存,而不需要任何数据的拷贝. (2)为了在多个进程间交换信息,内核专门留出了一块内存区,可以由需要访问的进程将 ...
- Android 界面间传参数
登陆页 //声明Intent对象,并启动 LoginActivity Activity Intent intent = new Intent(); intent.setClass(LoginActiv ...