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 " ...
随机推荐
- 单片机TM4C123学习(十):ADC采样模块
1.头文件 #include "tiva_adc.h" // ADC 2.引脚 3.初始化 // ADC初始化 // 光敏电阻(PE0)为通道3,存在序列0中,硬件平均为8个点 a ...
- 用PHP将Unicode 转化为UTF-8
function unescape($str) { $str = rawurldecode($str); preg_match_all("/(?:%u.{4})|&#x.{4};|& ...
- Median of Two Sorted Arrays
题目:https://leetcode.com/problems/median-of-two-sorted-arrays/ 算法分析 这道题的目的,是为了从两个有序列中找到合并序列之后的中位数,即两个 ...
- hadoop2.0单机安装
hadoop发行的版本:apache hadoop;HDP;CDH -----这里只使用apache hadoop---可以在网站hadoop.apache.org网站上找到 hadoop安装方式:自 ...
- c++中级 STL基础学习(二)
deque 和vector差不多,可以在前端后端插入,一般用deque取代vector,vector只能在后端插入push_back().deque还可以push_front(),但是deque后端插 ...
- vb.net加密解密方法
1.vb.net加密解密方法 Private Function getLicenseDate() As String Dim b() As Byte Dim path As String = Serv ...
- js-事件、正则表达式
AddEventListener()之中有三个参数,分别是(1)事件的名称(注:不要加on,例:click才是点击事件的名称)(2)需要执行的function(){} (3)布尔类型(false表示的 ...
- imageNamed、imageWithContentsOfFile、imageWithData
[UIImage imageNamed:ImageName]; 1.加载图片占据的内存较大 2.相同的图片只会加载一份到内存中,如果同时使用,使用同一个对象即可 3.当对象销毁,图片对象不会随着一起销 ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- 6.5 为什么Android用Java不用c实现?
C/C++过于底层,开发者要花很多的经历对C/C++的语言研究清楚,例如C/C++的内存机制,如果稍不注意,就会忘了开启或者释放.而Java的GC会自动处理这些,省去了很多的时间让开发者专注于自己的业 ...