[LeetCode] 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 (2014-12-06):
New test cases had been added. Thanks unfounder's contribution.
确认输入的字符串是否为一个数值,一系列的判断,主要是一些位置的判断:
- 输入前置空格
- 正负号
- 连续的数值,包括‘.’
- 符号e
- 正负号
- 连续数值,不包括'.'
- 后续空格
按上面的规则便行。
#include <iostream>
using namespace std; class Solution {
public:
bool isNumber(const char *s)
{
int idx =;
for(;s[idx]==' ';idx++);
if(s[idx]=='-'||s[idx]=='+') idx++;
int Point=,Num=;
for(;(s[idx]>=''&&s[idx]<='')||s[idx]=='.';idx++)
s[idx]=='.'?Point++:Num++;
if(Point>||Num<) return false;
if(s[idx]=='e'){
idx++;
if(s[idx]=='-'||s[idx]=='+') idx++;
Num=;
for(;s[idx]>=''&&s[idx]<='';idx++) Num++;
if(Num<) return false;
}
for(;s[idx]==' ';idx++);
return s[idx]=='\0';
}
}; int main()
{
char a[]="-e-";
Solution sol;
cout<<sol.isNumber(a)<<endl;
return ;
}
[LeetCode] Valid Number 确认是否为数值的更多相关文章
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [leetcode]Valid Number @ Python
原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...
- LeetCode——Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- Leetcode Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- LeetCode Valid Number 有效数字(有限自动机)
题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...
- leetcode - valid number 正则表达式解法
import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...
- [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 ...
随机推荐
- linux正则表达式企业级深度实践案例2
[root@redhat~]# sed -nr ' s#([ ^ : ]+) (: .* :) (/.*$)#\3\2\1#gp ' /etc/passwd
- eclipse中使用git上传项目
eclipse中使用git上传项目 先需要上传到本地仓库 先找到此选项打钩 再如下 再添加的属性则可以自动填充邮箱和密码 之后 右键选择import 点击找到git 选中 下一步 如果没有找到git ...
- 最新手机号正则表达式php包括166等号段
if(!preg_match("/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147)) ...
- A1083 List Grades (25)(25 分)
A1083 List Grades (25)(25 分) Given a list of N student records with name, ID and grade. You are supp ...
- 网络流 EK算法模板。
这篇博客讲得很好 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ...
- POJ 2311 Cutting Game(SG函数)
Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4806 Accepted: 1760 Desc ...
- Android 自定义debug.keystore
场景分析: 有时候,我们要使用第三方的服务,需要提供自己的包名以及keystore的sha1值,比如微信支付,百度地图,都需要包名和keystore的sha1值作为唯一标识.这时候我们测试的时候,如果 ...
- msconfig.exe
msconfig.exe 编辑 本词条缺少概述.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 中文名 微软系统配置实用程序 外文名 msconfig.exe 出品者 Micros ...
- 39、apk瘦身(转载)
本文转自::Android开发中文站 » 关于APK瘦身值得分享的一些经验 从APK的文件结构说起 APK在安装和更新之前都需要经过网络将其下载到手机,如果APK越大消耗的流量就会越多,特别是对于使用 ...
- MoveWindow() SetWindowPos()的区别与联系
敲代码时,突然发现有一个背景图片无法显示,百思不得其解,最终发现是MoveWindow() SetWindowPos()这两个函数的使用不当造成的. 这里把这两个函数的前世今生给分析一下. 先看Mov ...