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.

click to show spoilers.

Update (2014-12-06):
New test cases had been added. Thanks unfounder's contribution.


  确认输入的字符串是否为一个数值,一系列的判断,主要是一些位置的判断:
  1. 输入前置空格
  2. 正负号
  3. 连续的数值,包括‘.’
  4. 符号e
  5. 正负号
  6. 连续数值,不包括'.'
  7. 后续空格

按上面的规则便行。

#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 确认是否为数值的更多相关文章

  1. LeetCode: Valid Number 解题报告

    Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  2. [LeetCode] Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  3. [leetcode]Valid Number @ Python

    原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...

  4. LeetCode——Valid Number

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  5. Leetcode Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  6. LeetCode Valid Number 有效数字(有限自动机)

    题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...

  7. leetcode - valid number 正则表达式解法

    import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...

  8. [leetcode]65. Valid Number 有效数值

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  9. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

随机推荐

  1. mysql 删除 一天前 创建 的数据,计算时间差

    DELETE from table_name WHERE TIMESTAMPDIFF(SECOND ,CREATE_TIME,now() ) > 24*60*60 https://www.cnb ...

  2. CentOS Linux release 7.6.1810全新安装 Zimbra 8.8.12邮箱

    1.1  基础环境配置 1.1.1  主机名配置 [root@mail ~]# hostnamectl --static set-hostname mail.example.com [root@mai ...

  3. cmf5分页相关

    //分页配置在app/config.php 'paginate' => [ 'type' => '\cmf\paginator\Bootstrap', 'var_page' => ' ...

  4. 【工具】Sublime Text 自动保存功能

    经常需要所以要频繁用到"ctrl+s"保存还是挺麻烦的,所以有的人需要用到失去焦点自动保存功能,这里简单记录下 1.点击"Preferences"里的设置-用户 ...

  5. String java问题随笔

    1.字符串加密 设计思想: 每个字符都能够转化为整数型,也能将整数型转化为字符类型,这样我们在加密时候由于向后推3个,所以可以将字符转换为整形,然后加3,之后在将运算完的变量转化为字符后输出,就可以实 ...

  6. Python知识点入门笔记——Python的基本数据类型

    Python的数字分为4种类型:整数(int).浮点数(float).布尔值(bool).复数(complex). type()函数可以知道数据的类型,如type(233)是int型,type(233 ...

  7. stark组件前戏(3)之django路由分发的本质include

    django路由分发的三种方式 方式一: from django.urls import re_path, include urlpatterns = [ re_path(r'^web/', incl ...

  8. Find a path HDU - 5492 (dp)

    Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. mongoTemplate聚合操作Demo

    package com.tangzhe.mongodb.mongotemplate; import com.mongodb.BasicDBObject; import com.mongodb.DBOb ...

  10. js中基础数据类型

    变量声明 undefined //未定义只声明   var age; alert(name);function fc(a1,a2,a3) { //alert(a1); //alert(a2); //a ...