【leetcode】Valid Number
Valid Number
Validate if a given string is numeric.
Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
其他测试用例:
class Solution {
public:
enum TYPE
{
INVALID,
SPACE,
SIGN,
DIGIT,
DOT,
EXP
};
bool isNumber(const char *s) {
while(*s!='\0'&&*s==' ') s++;
if(*s=='+'||*s=='-') s++;
if(strlen(s)==) return false;
bool hasSign=false;
bool hasDigit=false;
bool hasDot=false;
bool hasExp=false;
TYPE preType;
TYPE type;
while(*s!='\0')
{
type=getType(s);
if(type==INVALID) return false;
if(preType==SIGN &&(type!=DIGIT&&type!=DOT)) return false;
if(preType==DOT&&(type!=DIGIT&&type!=SPACE&&type!=EXP))return false;
if(preType==EXP&&(type!=DIGIT&&type!=SIGN))return false;
if(preType==SPACE&&type!=SPACE)return false;
switch(type)
{
case SPACE:
preType=SPACE;
break;
case SIGN:
if(hasSign) return false;
else
{
if(preType!=EXP) return false;
hasSign=true;
preType=SIGN;
}
break;
case DIGIT:
hasDigit=true;
preType=DIGIT;
break;
case DOT:
if(hasDot||hasExp) return false;
else
{
hasDot=true;
preType=DOT;
}
break;
case EXP:
if(hasExp||!hasDigit) return false;
else
{
hasExp=true;
preType=EXP;
}
break;
}
s++;
}
if(preType==SIGN||preType==EXP||(!hasDigit&&hasDot)) return false;
return true;
}
TYPE getType(const char *s)
{
if(*s==' ') return SPACE;
else if(*s=='+'||*s=='-') return SIGN;
else if(isdigit(*s))return DIGIT;
else if(*s=='.')return DOT;
else if(*s=='e')return EXP;
else return INVALID;
}
};
【leetcode】Valid Number的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode】1178. Number of Valid Words for Each Puzzle
题目如下: With respect to a given puzzle string, a word is valid if both the following conditions are sa ...
- 【leetcode】Valid Triangle Number
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 【Leetcode】【Hard】Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
随机推荐
- MANIFEST.MF的用途(转载)
MANIFEST.MF的用途(转载) 可以用的上的有: 1. Main-Class 指定程序的入口,这样可以直接用java -jar xxx.jar来运行程序. 2. Class-Path 指定jar ...
- C#List的排序和简单去重总结
List集合在开发过程中很常见,经常我们要对该集合进行一系列操作,本文介绍如何将该集合内的元素进行排序,博主制作简单WinForm应用程序进行演示. 首先,我们来看一下c#泛型List提供的Sort方 ...
- 如何快速有效的修改java的环境变量
之前已经修改过jdk的环境变量,,,,在/etc/profile下,,, export JAVA_HOME=/usr/java/jdk1.7.0_67-cloudera export PATH=${J ...
- vim关于 引号和 括号的 高效操作-很好很强大的!
http://blog.csdn.net/bigshady/article/details/6019963 对括号匹配, 进行跳转, 使用的是%. 匹配的括号, 都会被高亮显示, 但是: 根据光标的 ...
- R-数据结构
目录 数据类型(模式) 字符型 数值型 逻辑型 整形 复数型(虚数) 原生型(字节) 数据结构 向量 矩阵 数组 数据框 列表 数据类型 数据结构 向量 用于存储数值型.字符型或逻辑型数据的一维数组 ...
- sqoop
http://blog.csdn.net/yfkiss/article/details/8700480 http://www.cnblogs.com/admln/p/sqoop1-99-4-javaa ...
- ACM2 斐波那契数列
描述 在数学上,斐波那契数列(Fibonacci Sequence),是以递归的方法来定义: F0 = 0 F1 = 1 Fn = Fn - 1 + Fn - 2 用文字来说,就是斐波那契数列由0和1 ...
- JSON 问题
{"statusCode":"300","message":"栏目插入出现故障==bannerInfoService.add 栏目 ...
- css的计数器
更多关于计数器的问题可以参考:https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Getting_Started/Lists
- Hanoi问题
#include<stdio.h>int main(){ int m; void hanoi(int n,char x,char y,char z); printf("input ...