[LintCode] Valid Number 验证数字
Validate if a given string is numeric.
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
LeetCode上的原题,请参见我之前的博客Valid Number。
class Solution {
public:
/**
* @param s the string that represents a number
* @return whether the string is a valid number
*/
bool isNumber(string& s) {
bool num = false, numAfterE = true, dot = false, exp = false, sign = false;
int n = s.size();
for (int i = ; i < n; ++i) {
if (s[i] == ' ') {
if (i < n - && s[i + ] != ' ' && (num || dot || exp || sign)) return false;
} else if (s[i] == '+' || s[i] == '-') {
if (i > && s[i - ] != 'e' && s[i - ] != ' ') return false;
sign = true;
} else if (s[i] >= '' && s[i] <= '') {
num = true;
numAfterE = true;
} else if (s[i] == '.') {
if (dot || exp) return false;
dot = true;
} else if (s[i] == 'e') {
if (exp || !num) return false;
exp = true;
numAfterE = false;
} else return false;
}
return num && numAfterE;
}
};
[LintCode] Valid Number 验证数字的更多相关文章
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 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" => ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Swift]LeetCode65. 有效数字 | Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
随机推荐
- C语言函数的读写
文件打开关闭函数:fopen()和fclose() <FILE *fopen(char *filename, char *mode)| int fclose(FILE *fp)> 字符读写 ...
- Bag-of-words模型
Bag-of-words模型是信息检索领域常用的文档表示方法.在信息检索中,BOW模型假定对于一个文档,忽略它的单词顺序和语法.句法等要素,将其仅仅看作是若干个词汇的集合,文档中每个单词的出现都是独立 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- [Liferay6.2]Connect to ajax.googleapis.com …… timed out
启动liferay 6.2 tomcat之后,后台会报一大段的异常信息,主要异常信息如下: -- :: org.apache.shindig.gadgets.http.BasicHttpFetcher ...
- cocos2dx游戏开发——微信打飞机学习笔记(一)——开发准备
一.环境的搭建 1.Windows开发准备: (1)软件下载及安装 •下载Cocos2d-x 最新版本:http://www.cocos2d-x.org/download 或者从Cocos2d-x G ...
- 常用chrome插件推荐
下面打红色的2个强烈推荐使用: FQ的: https://chrome.google.com/webstore/detail/ecross-free/njdjpgffklilbojbobbfecfcg ...
- Android拓展系列(11)--打造Windows下便携的Android源码阅读环境
因为EXT和NTFS格式的差异,我一直对于windows下阅读Android源码感到不满. 前几天,想把最新的android5.0的源码下下来研究一下,而平时日常使用的又是windows环境,于是专门 ...
- position-relative 的问题
对100%宽度的元素0001添加position-relative属性,如果再给left/right属性,可能会导致0001元素超出其父盒子的范围.如果盒子0001的父级元素是body,可能会出现滚动 ...
- js控制密码的显示与隐藏实例
原理是建立2个input,一个type是text,一个type是password.在点击按钮时,这两input个的显示状态与val()的值在切换. html: <!DOCTYPE html> ...
- Fibonacci
20145218 <Java程序设计>第01次实验报告 实验内容 简单使用命令行 撰写简单的Hello.java程序,使用命令行编译.运行.编译命令javac -d Hello.java, ...