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 " ...
随机推荐
- IOS设计模式第一篇之MVC
设计模式的好处:我们可以写出容易理解,重用度很高的代码.降低代码的耦合度,符合软件工程的思想. 设计模式主要分为三类:创造型的:单例和抽象工厂.结构类型的: MVC Decorator, Adapt ...
- iOS button 里边的 字体的 摆放
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; button.titleEdgeInsets ...
- iOS10适配及Xcode8配置
一.证书管理 用Xcode8打开工程后,比较明显的就是下图了,这个是苹果的新特性,可以帮助我们自动管理证书.建议大家勾选这个Automatically manage signing(Ps.但是在bea ...
- unreal3之FName及潜在bug
FName是unreal3里对字符串高效处理的一种机制 基本原理就是把字符串hash存起来,然后每个字符串就只需要用一个数组索引值来表示了 FName的属性: NAME_INDEX Index; IN ...
- javascript第四节其它引用对象
单体对象 Global对象(全局)这个对象不存在,无形的对象 其内部定义了一些方法和属性:encodeURL.encodeURIComponent.decodeURI.decodeURICompone ...
- Add sharing to your app via UIActivityViewController
http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/ April 4, 2014 Ev ...
- LeetCode(76) Minimum Window Substring
题目 Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- Vrapper-Eclipse的vim插件安装方法
Vrapper是一款Eclipse的插件,使在Eclipse下编辑文档时可以像使用Vim一样. 它有两种安装方法,在线安装和安装包安装: 在线安装: 打开Eclipse,Help->Instal ...
- 【枚举】bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏
#include<cstdio> using namespace std; #define N 10001 int n; int v[N<<1],first[N],next[N ...
- 如何制作u盘启动盘
1,下载windows系统 许多人下载windows时会出现各种版本,我推荐在这里下载 你用上面的地址进行下载,一般用迅雷吧.下载结束你就有了镜像文件了. 2,下载安装碟软通 那 ...