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. Eclipse启动Tomcat时,45秒超时解决方式

    Eclipse启动Tomcat时,45秒超时解决方式 在Eclipse中启动Tomcat服务器时,经常由于系统初始化项目多,导致出现45秒超时的Tomcat服务器启动错误.  一般通过找到XML配置文 ...

  2. 添加用户到 sudo

    sudo 简介: 在 Linux 中系统管理员可以通过 sudo 实用程序让用户或组能够作为另一个用户运行命令.换句话说,可以分派命令特权,而不需要另一个用户的密码.root 用户通过在 /etc/s ...

  3. 20145324王嘉澜 《网络对抗》进阶实践之 shellcode注入和Return-to-libc攻击深入

    Shellcode注入 •Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将堆栈的返回地址利用缓冲区溢出,覆盖成为指向 shellcode的地址 •实验参 ...

  4. Asterisk1.8 转码策略分析

    最近在修改asterisk转码和编码协商的问题,发现asterisk的转码策略的选择还是有些问题的(基于1.8.9.3版本).——————————————相关的CLI命令转码路径的调试命令:core ...

  5. STM32定时器的预装载寄存器与影子寄存器之间的关系【转】

    首先转载:   STM32定时器的预装载寄存器与影子寄存器之间的关系 本文的说明依据STM32参考手册(RM0008)第10版:英文:http://www.st.com/stonline/produc ...

  6. MIMO雷达比幅单脉冲测角精度分析(系统工程与电子技术)

    MIMO雷达比幅单脉冲测角精度分析(系统工程与电子技术)

  7. nohup 日志切割

    最近遇到日志切割的问题,即程序是通过命令: nohup python *.py & 放到后台执行的,这样程序的日志输出到了nohup自动生成的nohup.out文件. 问题就来了,nohup. ...

  8. UVa 127 纸牌游戏(栈)

    https://vjudge.net/problem/UVA-127 题意: 按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠.游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数 ...

  9. MAC下Java安装之后的路径

    pwd /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home 安装好jdk之后,就开始配置环境变量了. 首先,在终端输入 s ...

  10. ORA-01940:无法删除当前已连接的用户

    当时时间比较着急,直接采取了釜底抽薪的办法——拔网线,这招很管用,再次执行drop user就成功了.考虑到以后不一定所有场合都允许你采取这种简单粗暴的办法,就在虚拟机上重现了这个报错,寻求一种较为温 ...