LeetCode 65 Valid Number
(在队友怂恿下写了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的更多相关文章
- [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 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- [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 " = ...
随机推荐
- 在centos下部署docker内网私服
Docker内网私服:docker-registry with nginx & ssl on centos docker-registry既然也是软件应用,自然最简单的方法就是使用官方提供的已 ...
- WPF在XAML中Binding使用StringFormat属性
1. 绑定Currency, 如果没有字符的话, =后面需要先加入{}. 不加的话会出问题. 1 <TextBlock Text="{Binding Amount, StringFor ...
- Java Web利用POI导出Excel简单例子
采用Spring mvc架构: Controller层代码如下 @Controller public class StudentExportController{ @Autowired private ...
- Self Organizing Maps (SOM): 一种基于神经网络的聚类算法
自组织映射神经网络, 即Self Organizing Maps (SOM), 可以对数据进行无监督学习聚类.它的思想很简单,本质上是一种只有输入层--隐藏层的神经网络.隐藏层中的一个节点代表一个需要 ...
- 性能指标TP99之我解
首先给出Google到的答案: The tp90 is a minimum time under which 90% of requests have been served. tp90 = top ...
- Android Studio导入Eclipse项目和一些常见的问题
Android Studio版本 Eclipse项目工程:一个主工程,一个Emojicon依赖库. 有两种方式导入Eclipse工程: 1.兼容Eclipse 2.全新的Android Gradle ...
- Object C学习笔记26-文件管理(二)
上一篇简单的介绍了如何获取文件属性,删除,拷贝文件等,本文继续记录Object C中文件IO操作. 一. 获取文件的执行主目录 在Object C中提供了一个方法 NSHomeDirectory() ...
- Visual Studio调试
一:C# CODING 技巧 1:TODO 然后 CTRL + W + T,打开任务列表,选中 Comments,就会显示所有待做的任务 2:打开所在的文件夹 右键单击任何一个文件选项卡, 选择&qu ...
- 标准IDispose模式浅析
DoNet资源 众所周知,.Net内存管理分托管资源和非托管资源,把内存中的对象按照这两种资源划分,然后由GC负责回收托管资源(Managed Resource),而对于非托管资源来讲,就需要程序员手 ...
- 百度地图 api 功能封装类 (ZMap.js) 新增管理事件功能 [源码下载]
ZMap 功能说明 ZMap.js 本类方法功能大多使用 prototype 原型 实现: 包含的功能有:轨迹回放,圈画区域可编辑,判断几个坐标是否在一个圆圈内,生活服务查询,从经纬度获取地址信息,地 ...