Leetcode Valid Number
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的更多相关文章
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [leetcode]Valid Number @ Python
原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...
- LeetCode——Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- LeetCode Valid Number 有效数字(有限自动机)
题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...
- leetcode - valid number 正则表达式解法
import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...
- [LeetCode] Valid Number 确认是否为数值
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
随机推荐
- win7使用iis并搭建 图片服务器
1.打开控制面板 2.程序-卸载程序 3.点击左边的 打开或关闭windows功能 4.如下图所示,找到internet信息服务勾选.顺便把FTP服务器也全部勾选了,后面会用到 5.进入 控制面板 – ...
- [java] 可视化日历的实现(基于Calendar类 )
写在前面 博文安排顺序如下 1.写在前面 2.源码 3.思路 4.相关知识 该小程序是对Date类及其相关类的复习 要求如下图:实现可视化日历 实现思路 1.先从键盘输入指定格式的字符串(str)2. ...
- nyoj 下三角矩阵
Problem A 下三角矩阵 时间限制:1000 ms | 内存限制:65535 KB 描述 给定一个由0和1组成的矩阵.只允许交换相邻的两行,要把矩阵转化成下三角矩阵(主对角线上方的元素都是0 ...
- C# 使用Silverlight toolkit Chart
一.基础介绍 Silverlight ToolKit是微软发布的基于Microsoft-Public License(MS-PL)许可协议的控件集.MS-PL许可协议允许商业或非商业的发布,所以我们可 ...
- Linux下的Apache和PHP安全设置,如何开启PHP的安全模式
Linux下的Apache和PHP安全设置 PHP安全模式开启,PHP5.3将不再有安全模式. (1) safe_mode:以安全模式运行php; 在php.ini文件中使用如下 safe_mode ...
- 01OC概述
目前来说,Objective-C(简称OC)是iOS开发的核心语言,在开发过程中也会配合着使用C语言.C++,OC主要负责UI界面,C语言.C++可用于图形处理.特点如下: 一.OC基于C语言 C语言 ...
- delphi xe4 ini文件不能读取的解决方法
今天发现用inifiles下 tinifile.readstring方法突然不能读数据了,结果把ini文件格式由utf-8改成unicode后就能正常读取了.
- IIS相关知识
1.在web.config中,iis6使用<system.web>下配置项,iis7使用<system.webServer>下配置项 2.<httpHandlers> ...
- testng 教程
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- c#接口
//接口中方法 属性 事件等默认都是public 不允许用修饰符修饰 public interface IEventInterFace { string this[int index] { get; ...