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.

只需要考虑正负号,小数点,指数e符号,数字四种可能,除了它们之外的字符,程序返回false。

  • 正负号:只能出现在第一个字符或者e后面,不能出现在最后一个字符
  • 小数点:字符串不能只含有小数点,也不能只含正负号和小数点,小数点不能在e或者小数点后
  • e:e不能出现在第一个字符,也不能出现在最后一个字符,e前面不能没有数字,也不能有e
class Solution {
public:
bool isVaild(char c){
if(c=='+' || c=='-' || c == '.' || c=='e' || c>=''&& c <= '') return true;
else return false; } bool isNumber(const char *s) {
string str(s);
bool res = false;
size_t pos = str.find_first_not_of(" ");
if(pos!=string::npos) str=str.substr(pos); //去掉前端空格
else str="";
pos = str.find_last_not_of(" ");
str=str.substr(,pos+); //去掉后端空格
if(str == "") return res;
bool hasSign = false, hasDot = false, hasExp = false, hasDigit = false;
int len = str.length();
for(int i = ; i < len ;++ i){
char ch = str[i];
if(!isVaild(ch)) return false;
switch(ch){
case '+':
case '-':
//不在第一个或者e后面;在最后一个字符
if((i!= && str[i-]!='e') || i== len-) return false;
else hasSign = true;
break;
case '.':
//只有一个字符的情况;只有符号和点;在e和点之后
if(len == || (len == && hasSign) || hasExp || hasDot) return false;
else hasDot = true;
break;
case 'e':
//出现在第一个或最后一个;前面没有数字;前面有e
if(i == || i == len- || !hasDigit || hasExp) return false;
else hasExp = true;
break;
default:
hasDigit = true;
break;
}
}
return true;
}
};

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 有效数字(有限自动机)

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

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

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

  7. [LeetCode] Valid Number 确认是否为数值

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

  8. 【LeetCode】65. Valid Number

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

  9. 【leetcode】Valid Number

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

随机推荐

  1. My97DatePicker

    http://www.my97.net/index.asp <input id="txtDate" class="Wdate" type="te ...

  2. php提供更快的文件下载

    在微博上偶然看到一篇介绍php更快下载文件的方法,其实就是利用web服务器的xsendfile特性,鸟哥的博客中只说了apache的实现方式,我找到了介绍nginx实现方式的文章,整理一下! let' ...

  3. curl命令使用

    curl命令可以用来构造http请求.参数有很多,常用的参数如下: 通用语法:curl [option] [URL...]在处理URL时其支持类型于SHELL的名称扩展功能,如http://www.j ...

  4. vertx verticle

    以下内容为随手记的,若看客不知鄙人所云,还请原谅则个.............. 公司用的vertx,在国内,这还是款比较年轻的框架,你也可以把他当做一个工具,官网上的说法是: Vert.x is a ...

  5. Android 网络编程

    HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...

  6. mysql遇到锁表常用命令

    出现 waiting for table metadata lock 锁表的解决方法 1. show processlist; kill xxx; //xxx 为会话id 2.查询是否有未提交的事物 ...

  7. PHP变量入门教程(2)超全局变量,总共9个

    PHP 超全局变量 $GLOBALS 包含一个引用指向每个当前脚本的全局范围内有效的变量.该数组的键标为全局变量的 名称.从 PHP 3 开始存在 $GLOBALS 数组. $_SERVER 变量由 ...

  8. 浅谈ASM中的SLB

    接触Azure几个月,总想写点什么,迟迟没有动笔,一是怕自己技术粗鄙,写的东西会令人捧腹,二是工作原因,时间比较匆忙,在此再次声明,以下写的东西都是我个人看法,若有不足,请多多包涵!!! 情景是这样的 ...

  9. Qml 写的弹出层控件

    QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...

  10. [转]MyEclipse 里查看jar文件源码

    在开发过程中,有时候需要查看jar文件的源码,这里讲解如何设置.  选中某一个jar文件,如我这里选中的是struts2-core-2.1.6.jar,然后右键-->Properties--&g ...