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.

bool isNumber(string s) {
int l = s.length(), i;
bool flag=, point[]={,}, num[]={,};
for(i=; s[i] == ' '; i++);
for(; i<l; i++)
{
if(s[i] == ' ')
break;
if(s[i] == '-' || s[i] == '+')
{
if(num[flag] == true || point[flag] == true)
return false;
}
else if(isdigit(s[i]))
num[flag] = true;
else if(s[i] == '.')
{
if(point[flag] || flag == true)
return false;
point[flag] = true;
}
else if(s[i] == 'e')
{
if(flag == )
return false;
if(num[] == false)
return false;
flag = ;
}
else
return false;
}
for(;i<l;i++)
{
if(s[i]!=' ')
return false;
}
if(!num[] && !num[])
return false;
if(flag && !num[])
return false;
return true;
}

测试用例:

"0e" -- false

". 1" -- false

"-1." -- true

".-4" -- false

"+.8" -- true

"6e6.5" -- false

65. Valid Number *HARD*的更多相关文章

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

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

  2. 【LeetCode】65. Valid Number

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

  3. leetCode 65.Valid Number (有效数字)

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

  4. [LeetCode] 65. Valid Number 验证数字

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

  5. Leetcode 65 Valid Number 字符串处理

    由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...

  6. LeetCode 65 Valid Number

    (在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...

  7. 65. Valid Number

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

  8. 【一天一道LeetCode】#65. Valid Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...

  9. 65. Valid Number 判断字符串是不是数字

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

随机推荐

  1. centos 安装 ffmpeg

    使用yum方式安装ffmpeg: 先安装Nux Dextop仓库: Nux Dextop库依赖于EPEL库,所有要先安装EPEL库(需要管理员权限). 如果安装过则跳过. $ su root $ yu ...

  2. 关于cp命令的编写

    关于cp命令的编写 娄老师在课上详细的讲了命令who的编写过程~对此,我很有启发!于是想亲自动手试试~ 有什么不足的地方请大家提出来! Learning by doing ~ 做中学,真的只有自己动手 ...

  3. 20145208蔡野 《网络对抗》逆向及BOF基础实践

    20145208蔡野 <网络对抗>逆向及BOF基础实践 逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函 ...

  4. C++ VS2013环境编译使用sqlite数据库全过程

    转载:http://www.cnblogs.com/imoon/archive/2012/11/30/2796726.html 转载:https://blog.csdn.net/hjm4702192/ ...

  5. 在Android studio中到入Eclipse

    由于无法在AS中直接导入Eclipse的原始包,所以需要先把Eclipse的包导出成Gradle包,这个Gradle包可以别两个环境识别. 1.在Eclipse中导出Gradle包.选择需要从Ecli ...

  6. Python3基础 bool类型变量赋值

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Git入门私房菜

    昨天下午参考廖雪峰的博客和其他一些文章,简单了解了一下传说中的Git,发现常见用法入门还是挺容易上手的,在此做一些笔记,方便以后查阅和复习. Git安装 Linux sudo apt-get inst ...

  8. P3066 [USACO12DEC]逃跑的BarnRunning Away From

    目录 题目 思路 错误&&注意 代码 题目 luoguP3066 思路 虽说这个题目有多种做法,但 左偏树算法: 我们发现这个合并的时候并不好合并,因为存的值不是固定的 那我们是不是可 ...

  9. IDEA快捷键复习使用

    https://www.jetbrains.com/help/idea/meet-intellij-idea.html 快捷键可以极快地进行代码编辑整理,在IDEA的快捷键中,除了有几个好像特别难按之 ...

  10. 前端验证用户登陆状态(vue.js)

    首先用户需要进行登陆(请求登陆接口),接口请求成功之后后台会返回对应的用户信息(可以把用户信息存放在浏览器缓存中),并且后台会设置浏览器的cookie值(可以在network->header-& ...