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 ...
随机推荐
- 命令查看java的class字节码文件、verbose、synchronize、javac、javap
查看Java字节码 1 javac –verbose查看运行类是加载了那些jar文件 HelloWorld演示: public class Test { public static void main ...
- linux查看磁盘io的几种方法
怎样才能快速的定位到并发高是由于磁盘io开销大呢?可以通过三种方式: 第一种:用 top 命令 中的cpu 信息观察 Top可以看到的cpu信息有: Tasks: 29 total, 1 runnin ...
- Corel Painter 15在Surface Pro 4下开启笔触压力感应
之前一直是用Wacom的板子,所以只需要下载Wacom板子相应的驱动安装即可就能在PS和Corel Painter中开启压力感应来调节笔触出线的粗细.Surface Pro 4的笔是支持压力感应的,但 ...
- linux 下的clock_gettime() 获取精确时间函数
#include <time.h> int clock_gettime(clockid_t clk_id, struct timespec* tp); clock_gettime() 函数 ...
- KT vs SKT [20160816]
KT:索尔 SKT:茂凯,塔里克,卡西奥佩娅 普朗克+烬,大招开团. 塔里克保护,眩晕.
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
- Release时error c1083 无法打开包括文件
Release时error c1083 无法打开包括文件, 但Debug时没事. 项目里面包含了其实项目的头文件, 头文件目录就放在项目下面, 这个头文件里面调用其它头文件, 采用的是<xx/y ...
- VMware Workstation(虚拟机)v10.0.1 简体中文破解版
http://www.xp510.com/xiazai/ossoft/desktools/22610.html
- 如何设置a标签的宽高,如何使a标签的文字垂直居中
通常情况下a标签是没有宽高的,设置 width 和 height 没有作用. 若要使用 width 和 height,需要把a标签转为块级元素,即:display:block|inline-block ...
- Hibernate day04笔记
整合log4j(了解) slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合. 整合导入jar包 log4j 核 ...