(在队友怂恿下写了LeetCode上的一个水题)

传送门

题意

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.


Solution

题意是判断一个串是不是合法的实数(double literal),实际上是想让你实现 istream::istream &operator>>(const double &)(针对C++而言)。

我们当然可以 hand-code 出这个函数,但是更好的做法的利用 istream 类的某些 flag(member function)以其人之道还治其人之身。

Implementation

 class Solution {
public:
bool isNumber(string s) {
int b=, e=s.size();
for(; isspace(s[b]); b++);
for(; isspace(s[e-]); e--);
string t=s.substr(b, e-b);
if(*t.rbegin()!='.'&&!isdigit(*t.rbegin())) return false;
if(*t.rbegin()=='.'&&!isdigit(*(t.rbegin()+))) return false;
stringstream x(t);
double y;
x>>y;
return x.eof();
}
};

这大概是目前为止最短的C++实现了(如果不用regular expression的话)。。。(逃)

如果C++中double的范围充分大,还可以实现得更短:

 class Solution {
public:
bool isNumber(string s) {
int b=, e=s.size();
for(; isspace(s[b]); b++);
for(; isspace(s[e-]); e--);
stringstream x(s.substr(b, e-b));
double y;
x>>y;
return !x.fail() && x.eof();
}
};

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 字符串处理

    由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...

  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. Java构建

    大部分Java项目都是相似的,使用Gradle插件,能够抽象出这些步骤,这样就不必为每个Java项目都编写Gradle的project和task了 引入java插件 apply plugin: 'ja ...

  2. poj 3352

    Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11215 Accepted: 5575 De ...

  3. Java中的IO流系统详解(转载)

    摘要: Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java ...

  4. 在线运行Javascript,Jquery,HTML,CSS代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xht ...

  5. 挂多个class还是新建class —— 多用组合,少用继承

    用css实现下面的效果图. 方案一 <style type="text/css"> .myList1 { border: 1px solid #333; padding ...

  6. easyui-combobox的取值问题

    例子:<select id="cc" class="easyui-combobox" name="cc" style="wi ...

  7. 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表

    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法 IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Int ...

  8. CSS 实现加载动画之一-菊花旋转

    最近打算整理几个动画样式,最常见的就是我们用到的加载动画.加载动画的效果处理的好,会给页面带来画龙点睛的作用,而使用户愿意去等待.而页面中最常用的做法是把动画做成gif格式,当做背景图或是img标签来 ...

  9. 一款漂亮实用的Android开源日期控件timessquare

    这个开源控件可以兼容到SDK8版本,可以自定义显示的年月日,以及时间范围,如图 如果我们只想显示两个月的日期选择区间: final Calendar month = Calendar.getInsta ...

  10. C#出题库项目的总结(2)

    前记:好吧好吧,我好好的自我检讨,这个总结拖了这么久才来写,而且一周多没有看技术相关的东西,实在罪过,不过因为想做的事情太多,所以时间的分配确实是一个很严肃的问题,不是时间不够用,是我自己没有做好时间 ...