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. php提供更快的文件下载

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

  2. C#学习笔记

    1.C#中[],List,Array,ArrayList的区别 [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList ...

  3. 1、Jsp页面

    一.JSP(java server page):是以Java语言为基础的动态网页生成技术. 1.特点: a).以 .jsp 为后缀的文本文件,不需要编译(相对于程序猿来说不需要编译) b).以html ...

  4. nyoj220 推桌子(贪心算法)

    这道题太坑了,from 和to有可能写反,还得正过来: 推桌子 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The famous ACM (Advanced Co ...

  5. 一个asp采集程序

    <% if request.QueryString="" then url="http://www.hbcz.gov.cn:7001/XZQHQueryWAR/xx ...

  6. iphone的click导致div变黑

    -webkit-tap-highlight-color这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现一个半透明的灰色背 ...

  7. Qt界面中嵌入其他exe程序的界面,使用Qt5

    下面用一个小例子来演示如何在Qt的界面中嵌入其他exe程序的界面,最终效果如下图所示.本文参考了 http://blog.csdn.net/jiaoyaziyang/article/details/4 ...

  8. Entity framwork的数据库分页

    网上查了很多,原以为多么复杂的事情,其实很简单: list = list.OrderBy(orderBy, ascending).Skip((pageIndex - 1) * pageSize).Ta ...

  9. .NET LINQ 限定符操作

    限定符操作      限定符运算返回一个 Boolean 值,该值指示序列中是否有一些元素满足条件或是否所有元素都满足条件. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表 ...

  10. (转)CDN——到底用还是不用?

    用CDN的七个理由 浏览器从服务器上下载css.js和图片等文件时都要和服务器连接,而大部分浏览器对同一个域名用于下载文件的并发连接数限制在4个,这意味着如果要下载第五个文件就必须等前四个文件中有一个 ...