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

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

主要问题在需求定义上吧

  1. class Solution {
  2. public:
  3. bool isNumber(const char *s) {
  4. if(s == NULL)
  5. return false;
  6.  
  7. int index = ;
  8. // remove heading spaces
  9. while(s[index] != '\0' && s[index] == ' ')
  10. index++;
  11.  
  12. // only has spaces
  13. if(s[index] == '\0')
  14. return false;
  15.  
  16. // check + or - allowed
  17. if(s[index] == '+' || s[index] == '-')
  18. index++;
  19.  
  20. // remove tailing spaces
  21. bool hasSpace = false;
  22. int tailIndex = ;
  23. for(int i = index; s[i] != '\0'; i++)
  24. {
  25. if(s[i] == ' ')
  26. {
  27. if(hasSpace == false)
  28. tailIndex = i - ;
  29. hasSpace = true;
  30. continue;
  31. }
  32. else if(hasSpace && s[i] != ' ')
  33. return false;
  34.  
  35. if(hasSpace == false)
  36. tailIndex = i;
  37. }
  38.  
  39. // 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
  40. bool hasNum = false;
  41. bool hasDot = false;
  42. bool hasE = false;
  43. for(int i = index; i != tailIndex + && s[i] != '\0'; i++)
  44. {
  45. if(s[i] >= '' && s[i] <= '')
  46. hasNum = true;
  47.  
  48. else if(s[i] == '.')
  49. {
  50. if(hasDot || hasE)
  51. return false;
  52.  
  53. hasDot = true;
  54. }
  55. else if(s[i] == 'e')
  56. {
  57. if(hasE || hasNum == false)
  58. return false;
  59. hasE = true;
  60. hasNum = false;
  61. }
  62.  
  63. else if(s[i] == '+' || s[i] == '-')
  64. {
  65. if(!(i > && s[i-] == 'e'))
  66. return false;
  67. hasNum = false;
  68. }
  69. else
  70. return false;
  71. }
  72.  
  73. return hasNum;
  74. }
  75. };

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. Spring @Service生成bean名称的规则

    今天碰到一个问题,写了一个@Service的bean,类名大致为:BKYInfoServcie.java dubbo export服务的配置: <dubbo:service interface= ...

  2. 整合Apache+PHP教程

    首先修改Apache的配置文件,让Apache支持解析PHP文件,Apache配置文件在Apache安装目录的conf目录下的httpd.conf,打开此文件, 找到#LoadModule,在这个下面 ...

  3. Android主流UI开源库整理(转载)

    http://www.jianshu.com/p/47a4a7b99364 标题隐含了两个层面的意思,一个是主流,另一个是UI.主流既通用,一些常规的按钮.Switch.进度条等控件都是通用控件,因此 ...

  4. my first article

    BLOG:  http://codetask.org GIT:  http://git.oschina.net/codetimer

  5. ios系统的中arm指令集

    arm结构处理器,几乎所有的手机都基于arm,其在嵌入式系统中应用非常广泛. ARM 处理器因为低功耗和小尺寸而闻名,它的性能在同等功耗的产品中也很出色.这里我们注意一点,模拟器并不运行arm代码,软 ...

  6. myEclipse

    破解myEclipse 建立一个java项目,将reg.java放入,并且运行在控制台 输入账户 回车就会出现 序列号 菜单栏--->myeclipse-->substription in ...

  7. SmartWiki文档在线管理系统简介

    简介 SmartWiki是一款针对IT团队开发的简单好用的文档管理系统.可以用来储存日常接口文档,数据库字典,手册说明等文档.内置项目管理,用户管理,权限管理等功能,能够满足大部分中小团队的文档管理需 ...

  8. 【练习】移动数据----infile *

    要求: ①指定bad文件: ②挂在之前将目标表delete: ③导入的的数据在控制文件中. 1.创建目录对象: :: SYS@ORA11GR2>create or replace directo ...

  9. AngularJs 入门系列-2 表单验证

    对于日常的开发来说,最常见的开发场景就是通过表单编辑数据,这里涉及的问题就是验证问题. angularjs 内置已经支持了常见的验证方式,可以轻松实现表单验证. 1. 绑定 为了方便,我们在 $sco ...

  10. MAT内存问题分析定位

    MAT内存问题分析定位 1.下载安装MemoryAnalyzer工具. 2.使用DDMS将对应线程的内存日志导出来后,使用hprof-conv工具进行转换,用MAT打开转换后的hprof文件.