由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题。

题意:判断字符串是否是一个合法的数字

定义有符号的数字是(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 字符串处理的更多相关文章

  1. [leetcode]65. Valid Number 有效数值

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  2. leetCode 65.Valid Number (有效数字)

    Valid Number  Validate if a given string is numeric. Some examples: "0" => true " ...

  3. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  4. LeetCode 65 Valid Number

    (在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...

  5. [LeetCode] 65. Valid Number(多个标志位)

    [思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  8. 【一天一道LeetCode】#65. Valid Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...

  9. 65. Valid Number 判断字符串是不是数字

    [抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...

随机推荐

  1. Ubuntu 14.04下搭建Python3.4 + PyQt5.3.2 + Eric6.0开发平台

    引言 找了很多Python GUI工具集,还是觉得PyQt比较理想,功能强大跨平台,还支持界面设计器.花一天时间折腾了Ubuntu14.04(32位)+ Python3.4 + Qt5.3.2 + P ...

  2. wget的使用详解

    我在工作中, 经常下载遥感影像,每个影像都很大,使用普通的ftphelpe下载不太稳定,最终选择了linux下一款牛逼的下载工具wget,使用它的windows移植版本的.在此写此文,希望对和我一样保 ...

  3. ALV界面将可编辑字段值保存到内表中

    具体代码如下: data: lr_grid type ref to cl_gui_alv_grid.      data: l_valid type c.      read table gt_exc ...

  4. C#完美实现斐波那契数列

    /// <summary>         /// Use recursive method to implement Fibonacci         /// </summary ...

  5. Longest Palindromic Substring

    题目:https://leetcode.com/problems/longest-palindromic-substring/ 算法分析 这道题的解法有三种:暴力法.动态规划.Manacher算法.三 ...

  6. Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集

    实验室最近在做的项目要做ubuntu系统监控,要获得系统的一些信息并返回给web服务器. web服务器与ubuntu主机的通信我写的程序用的是socket,至于为什么不用java程序ssh到对应的主机 ...

  7. 拓扑排序 +Floyd(poj 1094)

    题目:Sorting It All Out 题意:字母表前n个字母,有m组他们中的大小关系,判断n个字母是否构成唯一序列: 1.Sorted sequence determined after xxx ...

  8. JS延时提示框

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...

  9. cell跳出动画

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  10. [SmartFoxServer概述]SFS2X栈平台

    SmartFoxServer 2X 栈平台 在这有一张SmartFoxServer 2X平台的鸟瞰图,接下来会简要介绍栈中的每个组件. 首先是服务器的核心——网络引擎(代号BitSwarm),它是用以 ...