【一天一道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 ...
随机推荐
- 让互联网更快,Server Push 特性及开启方式详解
过去 Nginx 并不支持 HTTP/2 的 Server Push 特性,幸运的是 Nginx 1.13.9 已支持该特性,详情介绍请移步 Nginx 官方博客. Server Push 这个特性是 ...
- java利用自定义类型对树形数据类型进行排序
前言 为什么集合在存自定义类型时需要重写equals和hashCode? 1.先说List集合 List集合在存数据时是可以重复的但是 当我们需要判断一个对象是否在集合中存在时这样就有问题了! 因为我 ...
- c++ Lambda表达式待修改
C++11引入了lambda表达式,使得程序员可以定义匿名函数,该函数是一次性执行的,既方便了编程,又能防止别人的访问. Lambda表达式的语法通过下图来介绍: 这里假设我们定义了一个如上图的lam ...
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
- Webpack 4 Tutorial: from 0 Conf to Production Mode
webpack 4 is out! The popular module bundler gets a massive update. webpack 4, what's new? A massive ...
- Node.js 虚拟机
稳定性: 3 - 稳定 可以通过以下方法访问该模块: var vm = require('vm'); JavaScript 可以立即编译立即执行,也可以编译,保存,之后再运行. vm.runInThi ...
- 豌豆夹Redis解决方案Codis源码剖析:Proxy代理
豌豆夹Redis解决方案Codis源码剖析:Proxy代理 1.预备知识 1.1 Codis Codis就不详细说了,摘抄一下GitHub上的一些项目描述: Codis is a proxy base ...
- ScrollView嵌套ListView后,进入页面不从顶部开始显示的问题解决
ScrollView嵌套ListView后,进入页面不从顶部开始显示的问题解决 首先,正常情况下,如果在ScrollView里嵌套ListView后,会发现ListView只显示1条数据 那么,为了解 ...
- TV Metro界面(仿泰捷视频TV版)源码解析
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52822499 前言:上一篇介绍了 ...
- The Zen Programmer
专注 何为专注 关于 休息 怎么睡觉 心无杂念 我的体会 自我分析 初学者心态 无我 不要设置职业目标 敏事慎言 正念 做自己的老板 玩物养志 结语 最近在研读Christian Grobmeier ...