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. java中常用的swing组件 (2013-10-27-163 写的日志迁移

    五种布局:   流式布局(FlowLayout)边界布局(borderLayout)网格布局(GridLayout)  盒子布局(BoxLaYout)  空布局(null)  常用的几种 卡片布局(C ...

  2. thinkphp3.2.3如何只改变地址url中的某一个分隔符,其它保持不变

    今天教大家一个关于使用thinkphp3.2.3改变只改变地址url中的某一个分隔符的方法,首先大家来看看这个地址! 它的原始地址应该是/Home/Index/index/page/2.html,那我 ...

  3. collections模块简介

    collections模块简介 除python提供的内置数据类型(int.float.str.list.tuple.dict)外,collections模块还提供了其他数据类型,使用如下功能需先导入c ...

  4. Mysql存储过程中的事务回滚

    create procedure test(in a int) BEGIN ; ;-- 异常时设置为1 START TRANSACTION; ,); ,); THEN ROLLBACK; ELSE C ...

  5. 如何提高STM32的学习效率

    时间如何安排 做任何事情前,习惯写一个计划——要在一个月内上手STM32! 没有计划的日子,每天早上醒来睁开眼睛,却不知道自己今天要干啥 计划和时间安排: 第一阶段:找感觉——谈及STM32,立即反应 ...

  6. 6 json和ajax传递api数据

    1 2 3 4 https://swapi.co/ <h1>Hello Reqwest!</h1> <script> var a = {} reqwest({ ur ...

  7. Python虚拟机类机制之descriptor(三)

    从slot到descriptor 在Python虚拟机类机制之填充tp_dict(二)这一章的末尾,我们介绍了slot,slot包含了很多关于一个操作的信息,但是很可惜,在tp_dict中,与__ge ...

  8. Django基于Pycharm开发之三[命名空间 与过滤器]

    关于命名空间的问题,在project项目中,我们可以设置路由类似于: from django.conf.urls import url,includefrom django.contrib impor ...

  9. loj2308 「APIO2017」商旅

    ref #include <iostream> #include <cstring> #include <cstdio> #include <queue> ...

  10. 谋哥:《App自推广》连载2直立人行走迁徙

    [谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代,是一个神奇的东西,它能够很快地把信息传播到其他地方,突破了短距离.然而能人的后代直立人学会了直立行走,他们开始走出非洲,到达遥远的中东.中国,还 ...