Leetcode 65 Valid Number 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题。
题意:判断字符串是否是一个合法的数字
定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的
合法的数字只有下列几种组合:(其中E可以大写)
有小数点和e
(n).(un)e(n)
.(un)e(n)
(n).e(n)
仅仅有小数点的
(n).(un)
.(un)
(n).
仅仅有e的
(n)e(n)
没有e的
(n)
只要分别判断这上面的8种情况就能得到正确的答案
更好的解法其实是一种叫做有限状态机的解法,下次再说这题的时候会有介绍
class Solution {
public:
bool isNumber(string s) {
int m = ;
for (; isspace(s[m]); m++);
int n = s.size() - ;
for (; isspace(s[n]); n--);
if (m == s.size()) s = "";
else s = string(s.begin() + m, s.begin() + n + );
int hasdot = s.find(".",);
int hase = s.find_first_of("eE", hasdot == string::npos ? : hasdot);
if (hasdot != string::npos && hase != string::npos){//有小数点和e
if (isSignNumber(string(s.begin(), s.begin() + hasdot))){
return isNumber(string(s.begin() + hasdot + , s.begin() + hase), ) && isSignNumber(string(s.begin() + hase+, s.end()));
}
else if (isUnsignNumber(string(s.begin() + hasdot + , s.begin() + hase))){
return isNumber(string(s.begin(), s.begin() + hasdot), ) && isSignNumber(string(s.begin() + hase + , s.end()));
}
else return false;
}
else if (hasdot != string::npos && hase == string::npos){//仅仅有小数点的
if (isSignNumber(string(s.begin(), s.begin() + hasdot))){
return isNumber(string(s.begin() + hasdot + , s.end()), );
}
else if (isUnsignNumber(string(s.begin() + hasdot + , s.end()))){
return isNumber(string(s.begin(), s.begin() + hasdot), );
}
else return false;
}
else if (hasdot == string::npos && hase != string::npos){//仅仅有e的
return isSignNumber(string(s.begin(), s.begin() + hase)) && isSignNumber(string(s.begin() + hase + , s.end()));
}
else return isSignNumber(string(s.begin() + hase + , s.end()));//没有e的
}
bool isNumber(string s, int type){
switch (type)
{
case :
if (s == "+" || s == "-" || s == "") return true;
return isSignNumber(s);
break;
case :
if (s == "") return true;
return isUnsignNumber(string(s.begin(), s.end()));
break;
default:
return false;
break;
}
}
bool isSignNumber(string s){
if (s.size() == ) return false;
if (s[] == '+' || s[] == '-') s = string(s.begin() + , s.end());
return isUnsignNumber(s);
}
bool isUnsignNumber(string s){
if (s.size() == ) return false;
for (string::size_type i = ; i < s.size(); ++i){
if (!isdigit(s[i])) return false;
}
return true;
}
};
Leetcode 65 Valid Number 字符串处理的更多相关文章
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- [LeetCode] 65. Valid Number(多个标志位)
[思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...
- 【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 ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- 65. Valid Number 判断字符串是不是数字
[抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...
随机推荐
- 开源PLM软件Aras详解二 汉化以及界面
Aras安装完毕之后,默认语言为英语,对于国内很多制造业并不适用,那么下面就来说说如何汉化 首先下载汉化包:zh-cn_languagepack-110v3.zip 步骤如下: 步骤1- 设定安装程序 ...
- grep 相关
1) -q 参数,本意是 Quiet; do not write anything to standard output. Exit immediately with zero status if ...
- 精妙SQL语句收集
SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作.(转) 一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数 ...
- C++中new和malloc的区别
原文:http://blog.163.com/ji_wei8888/blog/static/4868044620117361747282/ 1.new 是c++中的操作符,malloc是c 中的一个函 ...
- Java集合的运算之减法A-B
import com.sun.media.sound.SoftSynthesizer; import java.util.*; public class a123 { public static ...
- spring:bean的定义
一个完整的Bean的配置文件: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE beans ...
- echo中的逗号
作为一个自学PHP的小白来说,echo是用来打印字符串的,总结几种输入方式如下: $a = 'good morning'; echo $a; echo $a . 'boys'; echo " ...
- 『TCP/IP详解——卷一:协议』读书笔记——03
2013-08-17 17:31:49 1.7 分用 分用(Demultiplexing):这是一个过程——当目的主机收到一个以太网数据帧时,数据就开始从协议栈中由底向上升,同时去掉各层协议上的报文首 ...
- Linq to SQL 基础篇
LinqtoSqlDataContext Linq = new LinqtoSqlDataContext(ConfigurationManager.ConnectionStrings["sz ...
- 学一点Git--20分钟git快速上手
(图片已修复)在Git如日中天的今天,不懂git都不好意思跟人说自己是程序猿.你是不是早就跃跃欲试了,只是苦于没有借口(契机). 好吧,机会就在今天. 给我20分钟,是的,只要20分钟, 让你快速用上 ...