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 " ...
随机推荐
- clang format 官方文档自定义参数介绍(中英文)
官方文档:http://clang.llvm.org/docs/ClangFormatStyleOptions.html 中文 在代码中配置样式 当使用 clang::format::reformat ...
- Oracle题目
1. 创建一个函数fun_sal,该函数根据部门号获得该部门下所有员工的平均工资Create or replace function fun_sal(deptnos number)return var ...
- (转)VS2010启动调试时老是提示正在下载公共符号
VS2010启动调试时老是提示正在下载公共符号,下载一些.dll文件,点取消后也能继续调试,但特别慢. 解决方法:工具—选项,或者调试—选项和设置,将调试下的“启用 .NET Framework ...
- mysql 基础语法
以下为自己学习mysql 的一些笔记,以方便查询 目录 一. ALTER的 语法 二. 表的完整性约束 三. 索引的操作(mysql 数据库支持至少 16 个索引) 四. 视图的操作 五. 触发器的操 ...
- div mouseenter 事件在IE下无效
解决方法是在div style上加个背景色: background: rgba(0, 0, 0, 0);filter:alpha(opacity=0); background: rgba(0, 0, ...
- cinder backup
cinder 备份提供的驱动服务有: cinder/backup/drivers/ceph.py:def get_backup_driver(context): cinder/backup/drive ...
- 0801 am使用tp框架对数据库增删改查
增添数据,3种方法 function Text3() { $m=D("info"); //1.使用数组 $attr = array( "code"=>&q ...
- 文件和目录:access函数
access函数是按照实际用户ID和实际组ID进行访问权限测试的: #include <unistd.h> int access( const char *pathname, int mo ...
- Windows2008当桌面使用
因为需要32位系统,又想用8G内存. 一.提高开机速度 0 |" t7 A- d! `- A- R5 | 1.免除登录时按Ctrl+Alt+Del的限制 打开<开始> - & ...
- C++ 类的静态成员详细讲解
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...