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. ubuntu sudo update与upgrade的作用及区别

    ubuntu sudo update与upgrade的作用及区别 入门linux的同志,刚开始最迫切想知道的,大概一个是中文输入法,另一个就是怎么安装软件.本文主要讲一下LINUX安装软件方面的特点. ...

  2. winform 多线程编程

    参考资料: WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件) c# 使用定时器Timer

  3. 万恶的jar包冲突

    搭了个spring+struts2+mybatis的项目架子, 好久不用myEclipse和tomcat了,生疏了好多. 建议还是去百度一些框架整合的博客,直接使用博客里面给的jar包列表里的jar包 ...

  4. Alpha版本冲刺总结——曙光初现

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 项目预期计划 界面设计 androi ...

  5. 【Spring实战】—— 12 AspectJ报错:error at ::0 can't find referenced pointcut XXX

    今天在使用AspectJ进行注解切面时,遇到了一个错误. 切点表达式就是无法识别——详细报错信息如下: Exception can't find referenced pointcut perform ...

  6. mongoTemplate简单用法(增删改查)

    分页时查找数量: public long countSample(String id) { Query query = new Query(); if (StringUtil.hasText(id)) ...

  7. 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题

    一.问题描述 UITableView分割线要显示到最左端 查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置. @property (nonatomic) ...

  8. 日志解析LogParse启动参数配置

    -task task_stat1001to1010.yaml -log log4j_stat1001to1010.xml 用绝对路径

  9. C#创建socket服务

    1.新建windows服务,名称(WebSendMsgSocket,注意检查属性-版本号)  双击Service1.cs打开设计视图,在设计视图中右键,选择添加安装程序   安装serviceProc ...

  10. push 到下一界面明显卡顿的现象

    今天搭建界面,界面间的跳转使用了 push方法,结果发现界面间转换时有明显的卡顿现象,并没有做过数据请求之类的处理,所以感觉好神奇.以前好像也遇到过,当时在push 到的下级界面加个背景色.现在重新这 ...