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 ...
随机推荐
- 计算机视觉:关于视觉算法源码中常出现的imageLib库的使用指南
1.ReadImage(CImage &im, char* path)/ WriteImage(CImage &im, char* path) (1)将im强制转换为CByteImag ...
- [IIS]IIS扫盲(六)
一:聊天室 聊天室的种类有很多,免费的聊天室也有很多,这些聊天室的ASP源码从网上都可以下载得到,我们就以毒爱聊天室为版本来教大家做.好,大家先下载毒爱聊天室,当然,本站软件下载里就有下载,下载的是 ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- vbs脚本要求在cmd中输入输出用StdIn ,StdOut
Dim StdIn, StdOutSet StdIn = WScript.StdInSet StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStre ...
- finder文件目录跳转快捷键
finder文件目录跳转快捷键 command+shift+G
- Squid服务日志分析
Squid服务日志分析 Apache 和 Squid 是两种著名的代理缓存软件,但Squid 较 Apache 而言是专门的代理缓存服务器软件,其代理缓存的功能强大,支持 HTTP/1.1 协议,其缓 ...
- Excel—“撤销工作表保护密码”的破解并获取原始密码
您是否遇到过这样的情况:您用Excel编制的报表.表格.程序等,在单元格中设置了公式.函数等,为了防止其他人修改您的设置或者防止您自己无意中修改,您可能会使用Excel的工作表保护功能,但时间久了保护 ...
- 深入理解JS异步编程二(分布式事件)
PubSub模式 从原生的js角度,我们要监听某事件的方法就是利用addEventListener方法,但是当我们的页面趋于复杂,比如要向某个元素添加多个处理事件,那么就要用一个封装函数汇集多个处理函 ...
- php函数的引用返回
<?php function &test(){ static $b = 1; $b += 2; return $b; } $a = &test(); $a =8; $c = te ...
- 4.0以后的新布局方式GridLayout
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android=" ...