https://oj.leetcode.com/problems/valid-number/

判断给的串,是不是合理的 数字形式

主要问题在需求定义上吧

class Solution {
public:
bool isNumber(const char *s) {
if(s == NULL)
return false; int index = ;
// remove heading spaces
while(s[index] != '\0' && s[index] == ' ')
index++; // only has spaces
if(s[index] == '\0')
return false; // check + or - allowed
if(s[index] == '+' || s[index] == '-')
index++; // remove tailing spaces
bool hasSpace = false;
int tailIndex = ;
for(int i = index; s[i] != '\0'; i++)
{
if(s[i] == ' ')
{
if(hasSpace == false)
tailIndex = i - ;
hasSpace = true;
continue;
}
else if(hasSpace && s[i] != ' ')
return false; if(hasSpace == false)
tailIndex = i;
} // check only one . and e or digits allowed
     // . e can't both exists. and 8. is valid
     // before e and after e must has digits
     // + - before them must be e
bool hasNum = false;
bool hasDot = false;
bool hasE = false;
for(int i = index; i != tailIndex + && s[i] != '\0'; i++)
{
if(s[i] >= '' && s[i] <= '')
hasNum = true; else if(s[i] == '.')
{
if(hasDot || hasE)
return false; hasDot = true;
}
else if(s[i] == 'e')
{
if(hasE || hasNum == false)
return false;
hasE = true;
hasNum = false;
} else if(s[i] == '+' || s[i] == '-')
{
if(!(i > && s[i-] == 'e'))
return false;
hasNum = false;
}
else
return false;
} return hasNum;
}
};

LeetCode OJ-- Valid Number **@的更多相关文章

  1. 【leetcode】Valid Number

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

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

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

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

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

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

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

  5. LeetCode 65 Valid Number

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

  6. LeetCode OJ -Happy Number

    题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...

  7. [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

    class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...

  8. LeetCode OJ:Number of Islands(孤岛计数)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. LeetCode OJ:Number of 1 Bits(比特1的位数)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  10. LeetCode OJ 之 Number of Digit One (数字1的个数)

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

随机推荐

  1. 开源是一种态度、分享是一种精神 — FirApi发布、WeiXinApi更新

    在云计算盛行的年代,接触开发式的平台必不可少,因项目累积的代码也不少,之前本着"重复的事情自己做一次就够了,不需要其他人在重复为此工作."的想法发布了WeiXinApi.Boots ...

  2. HTC ONE里面一个非常奇怪的问题。。。调用kSOAP出错

    也是在某统计网站上看到了我们的APP爆出了这么一个bug: java.lang.NoSuchFieldError: No instance field headerOut of type [Lorg/ ...

  3. spring mvc定时任务的简单使用

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 说起定时任务,开发的小伙伴们肯定不陌生了.有些事总是需要计算机去完成的,而不是傻傻的靠我们自己去.可是好多人对定时器总感觉很陌 ...

  4. java基础回顾(八)——Queue

    今天回顾了下关于Queue的一些相关知识 我们可以看到,Deque也是一个接口,它继承了Queue的接口规范.其中LinkedList和ArrayDeque都是实现Deque接口,所以,可以说他们俩都 ...

  5. 一个页面从输入URL 到页面加载显示完成的过程中都发生了什么

    前端面试/笔试必考问题,越详细越好 先简单得讲: 浏览器根据请求的URL交给DNS域名解析,找到真实IP,向服务器发起请求: 服务器交给后台处理完成后返回数据,浏览器接收文件(HTML.JS.CSS. ...

  6. openStack镜像制作

    参考链接: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/OpenStack/page/Creating ...

  7. Linux下grep命令

    2.grep命令 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本 ...

  8. SQL Server 磁盘空间告急(磁盘扩容)转载

    一.背景 在线上系统中,如果我们发现存放数据库文件的磁盘空间不够,我们应该怎么办呢?新买一个硬盘挂载上去可以嘛?(linux下可以直接挂载硬盘进行扩容),但是我们的SQL Server是运行在Wind ...

  9. JavaWEB域对象

    PageContext: ServletRequest: HttpSession: ServletContext: void setAttribute(String name, Object valu ...

  10. HDU 5673 Robot ——(卡特兰数)

    先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和 ...