LeetCode OJ-- Valid Number **@
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 **@的更多相关文章
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [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
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Spring @Service生成bean名称的规则
今天碰到一个问题,写了一个@Service的bean,类名大致为:BKYInfoServcie.java dubbo export服务的配置: <dubbo:service interface= ...
- 整合Apache+PHP教程
首先修改Apache的配置文件,让Apache支持解析PHP文件,Apache配置文件在Apache安装目录的conf目录下的httpd.conf,打开此文件, 找到#LoadModule,在这个下面 ...
- Android主流UI开源库整理(转载)
http://www.jianshu.com/p/47a4a7b99364 标题隐含了两个层面的意思,一个是主流,另一个是UI.主流既通用,一些常规的按钮.Switch.进度条等控件都是通用控件,因此 ...
- my first article
BLOG: http://codetask.org GIT: http://git.oschina.net/codetimer
- ios系统的中arm指令集
arm结构处理器,几乎所有的手机都基于arm,其在嵌入式系统中应用非常广泛. ARM 处理器因为低功耗和小尺寸而闻名,它的性能在同等功耗的产品中也很出色.这里我们注意一点,模拟器并不运行arm代码,软 ...
- myEclipse
破解myEclipse 建立一个java项目,将reg.java放入,并且运行在控制台 输入账户 回车就会出现 序列号 菜单栏--->myeclipse-->substription in ...
- SmartWiki文档在线管理系统简介
简介 SmartWiki是一款针对IT团队开发的简单好用的文档管理系统.可以用来储存日常接口文档,数据库字典,手册说明等文档.内置项目管理,用户管理,权限管理等功能,能够满足大部分中小团队的文档管理需 ...
- 【练习】移动数据----infile *
要求: ①指定bad文件: ②挂在之前将目标表delete: ③导入的的数据在控制文件中. 1.创建目录对象: :: SYS@ORA11GR2>create or replace directo ...
- AngularJs 入门系列-2 表单验证
对于日常的开发来说,最常见的开发场景就是通过表单编辑数据,这里涉及的问题就是验证问题. angularjs 内置已经支持了常见的验证方式,可以轻松实现表单验证. 1. 绑定 为了方便,我们在 $sco ...
- MAT内存问题分析定位
MAT内存问题分析定位 1.下载安装MemoryAnalyzer工具. 2.使用DDMS将对应线程的内存日志导出来后,使用hprof-conv工具进行转换,用MAT打开转换后的hprof文件.