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 ...
随机推荐
- 如何查看oracle数据库告警日志
目标:查看alert日志 su - oracle cd $ORACLE_BASE/diag/rdbms/LXY/LXY/trace tail -100f alert_LXY.log 我的ORACLE_ ...
- 立体匹配:关于用OpenCV彩色化middlebury网站给定的视差
#include "XYZ.h" void readPFM(Mat_<float> &disp, float &scale, string path) ...
- ChartControl
<dxc:ChartControl Name="chartControl1"> <dxc:ChartControl.Diagram> ...
- 使用AJAX完成用户名是否存在异步校验
一.JSP代码: 1.事件触发:onblur 2.编写AJAX代码:向Action中提交,传递username参数 <script> function checkUsername(){ / ...
- 对SSH三大框架的理解
SSH框架一般指的是Struts.Spring.Hibernate,后来Struts2代替了Struts.最近5年,Struts2已经被Spring MVC代替,而Hibernate基本也被iBati ...
- (译)详解javascript立即执行函数表达式(IIFE)
写在前面 这是一篇译文,原文:Immediately-Invoked Function Expression (IIFE) 原文是一篇很经典的讲解IIFE的文章,很适合收藏.本文虽然是译文,但是直译的 ...
- AngularJS图片上传功能的实现
一.前言 前一段时间做项目时,遇到一个问题就是AngularJS实现图片预览和上传的功能,当时查阅文档(都是英文文档)折腾了很久才弄出来,现将整个流程整理出来,有需要的朋友可以参考一下,如果您有更好的 ...
- Squid服务日志分析
Squid服务日志分析 Apache 和 Squid 是两种著名的代理缓存软件,但Squid 较 Apache 而言是专门的代理缓存服务器软件,其代理缓存的功能强大,支持 HTTP/1.1 协议,其缓 ...
- JS settimeout 使用笔记
无参数使用方法: setTimeout(function_name,delay_time); 基本使用方法是逗号前是函数名字,不能带有 xxxx(),不然不执行: 但是很多函数都要带参数的,以下是解决 ...
- 苹果刷机相关开源代码(如iRecovery等)收集汇总(不断更新中...)
下面截图是在下面开源代码下使用VS2015修改部分代码后适配而成,可以在Windows平台上运行, 下载连接: http://pan.baidu.com/s/1i4zKGx3.