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 ...
随机推荐
- HttpWebRequest.GetResponse 方法 转载
GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...
- 【javascript基础】5、创建对象
前言 今天从家里回到了学校,在家呆了十天,胖了几斤的重量,又折腾回学校了,春节回家真是艰辛的路途.随便扯扯我的往返行程:为了省钱我没有选择直飞到长春往返都是到北京转的,这样我和女朋友可以节省4000块 ...
- EF 示例
EF有三种数据库访问方式,这里只介绍Code First. 1.DB First,类似Linq to sql中拖拽一个DB到方案中 2.Model First,没试过,不能自动生成数据库只能生成表 3 ...
- 远程登录,无法加载explorer
最近不知什么缘故,远程登录服务器时,无法登录到桌面了,只能用mstsc.exe /admin方式登录或者登录后按(CTRL+ALT+END)进入任务管理,新建运行explorer.exe才能登录到桌面 ...
- Archlinux KDE 添加中文语言包
From: https://wiki.archlinux.org/index.php/KDE_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29#.E8.AF.AD. ...
- 前端项目构建error
Refusing to install webpack as a dependency of itself 原因:package.json中,"name": "webpa ...
- 双击vbs时,默认cscript运行脚本
Dim obj_shellset obj_shell = createobject("wscript.shell")host = WScript.FullNameIf LCase( ...
- APK Downgrade Method working fine on LINE latest version 6.7.1
Line is one of the most popular messaging Apps, especially in Asia. On March 3 I downgraded the app ...
- FireDAC 超时
FireDAC 超时 Timeout expired 在Win10 正常. 在Win7 CB的DLL 正常,Delphi的DLL怎么会超时呢??? 果然是连接字符串错了.改为正确的就连接正常了!
- tz2txt的安装与使用
tz2txt是一个开源的小工具,用于把帖子的楼主发言保存为txt文件. 目前支持天涯社区.新浪论坛(大部分版块).百度贴吧. 本文介绍tz2txt的安装与使用. 本文目录: 一.下载.安装 二.使用t ...