【一天一道LeetCode】#65. Valid Number
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Validate if a given string is numeric.
Some examples:
“0” => true
” 0.1 ” => true
“abc” => false
“1 a” => false
“2e10” => trueNote: It is intended for the problem statement to be ambiguous. You should gather all requirements up front >before implementing one.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const >char * argument, please click the reload button to reset your code definition.
(二)解题
题目大意:判断一个string数是否为有效数
大致从以下三点考虑这个问题:
1、全为数字
2、存在有且仅有一个’e‘,这个时候需要判断前后的数字有效,如4e+,.e1
3、存在有些仅有一个’.’,注意指数不能为小数
做题时没有考虑到的特殊情况:
“e”,“4e+”,“.e1”
class Solution {
public:
bool isNumber(string s) {
int len = s.length();
if(len == 0) return false;
//剔除前面的空格
int st = 0 ;
while(st<len&&s[st] == ' ') st++;
int ed = len-1;
//剔除后面的空格
while(ed>=0&&s[ed] == ' ') ed--;
if(st>ed) return false;//如果全是空格就返回false
//符号
if(s[st]=='+'||s[st]=='-') st++;
//标志.和e的位置
int hase = -1;
int hasdot =-1;
//开始循环
for(int i =st ; i <= ed ;)
{
if(s[i]>='0'&&s[i]<='9') i++;
else if(s[i]=='e')
{
if(hase==-1) hase = i;//只能出现一个e
else return false;
if(i==st||i==ed) return false;//e在最前面或最后面返回false
i++;
if(s[i]=='+'||s[i]=='-') i++;//考虑指数带有符号
if(i>ed) return false;//指数非法,如10e+
}
else if(s[i]=='.')
{
if(hasdot==-1) hasdot = i;//只能有一个.
else return false;
i++;
if(ed-st==0) return false;//string为“.”的特殊情况
}
else return false;
}
if(hase!=-1&&hasdot!=-1){//判断e前面的数字有效
if(hase<hasdot) return false;//指数不能为小数
if(hasdot==st&&hase-hasdot==1) return false;//“.e1”的特殊情况,e前面数要有效
}
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 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- 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】#191. Number of 1 Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
随机推荐
- FileOutputStream&FileInputStream&异常的使用
FileOutputStream&FileInputStream&异常的使用 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真 ...
- ng-book札记——内置指令
Angular提供了一些内置指令(directive),这些添加在HTML元素(element)上的特性(attribute)可以赋予更多的动态行为. NgIf ngIf指令用于在某个条件下显示或者隐 ...
- iOS控制反转(IoC)与依赖注入(DI)的实现
背景 最近接触了一段时间的SpringMVC,对其控制反转(IoC)和依赖注入(DI)印象深刻,此后便一直在思考如何使用OC语言较好的实现这两个功能.Java语言自带的注解特性为IoC和DI带来了极大 ...
- Dynamics CRM2016 使用web api来创建注释时的注意事项
在使用wei api 创建注释的时候,有个字段需要注意下,就是下面图中的objectid字段,虽然它是个查找字段,但不能像普通的查找字段property@odata.bind来赋值 上代码,注意看倒数 ...
- Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...
- Scala:输入输出
http://blog.csdn.net/pipisorry/article/details/52902694 Scala基本输入输出 从屏幕上读取用户输入 有时候我们需要接收用户在屏幕输入的指令来处 ...
- Scala:函数和闭包
http://blog.csdn.net/pipisorry/article/details/52902271 Scala函数 Scala 有函数和方法,二者在语义上的区别很小.Scala 方法是类的 ...
- 剑指Offer——常用SQL语句、存储过程和函数
剑指Offer--常用SQL语句.存储过程和函数 常用SQL语句 1.在MySQL数据库建立多对多的数据表关系 2.授权.取消授权 grant.revoke grant select, insert, ...
- Dynamics CRM2016 Web API之Expand related entities & $ref & $count
本篇介绍两个关于1:N关系中通过主实体取关联子实体的api,这两个api会经常被用到而且比原来的odata方式更加方便,之前如果我们要取主实体下所有的关联实体的记录都是通过Retrieve Multi ...
- 凸函数与Jensen不等式
这个是在凸优化里面看的,在EM算法中看有用到,所以用latex写了篇回忆用的小短文,现在不会把latex产生的pdf怎么转变成放到这里的内容. 所以我选择直接贴图. 这个pdf可以在我的资源里找到. ...