【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 ...
随机推荐
- 网络广告术语CPC、CPM和CTR的含义和关系
1. CPC(Cost-per-click):对于广告主来说,就是每次点击(广告)的(付给网站主的)成本:对于媒体(或网站主)来说,就是用户每次点击(广告)(向广告主收取)的费用.可以用公 ...
- centos 7 DenyHosts 安装 防暴力破解ssh登陆
为了减少软件扫描ssh登陆 还是用这个比较好点 默认端口号22 也要改 登陆密码也不要使用 弱口令 123456 这样的 Description DenyHosts is a python prog ...
- centos 7.0 PHP 5.6.5 安装过程 (php+nginx)
php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...
- JDK自带方法实现AES对称加密
请看代码. 1 package jdbc.pro.lin; 2 3 import java.security.InvalidAlgorithmParameterException; 4 import ...
- winxp可以禁用的服务
要注意的是: 虽然某个服务你设置成了手动, 而且在services.msc中好像也没有启动, 但是并不表示这个服务不可以被启动 因为某些软件, 可能在程序内部进行了编程的设置, 它可以在内部去启动 服 ...
- jupyter notebook + pyspark 环境搭建
安装并启动jupyter 安装 Anaconda 后, 再安装 jupyter pip install jupyter 设置环境 ipython --ipython-dir= # override t ...
- flask 项目的开发经验总结
已经开发了几个flask项目, 是时候总结一下了, 这里涉及到项目源码的组织, 常用的包, 源码示例. =========================需要的 python 包有:========= ...
- 黑客攻防技术宝典Web实战篇(一)Web应用程序技术基础
在开展Web应用程序渗透测试之前请先了解下面列出的这些内容,如果不是很懂的话,请读David Gourley & Brian Totty的HTTP权威指南也叫HTTP:The Definiti ...
- 郝斌C语言代码
#include<stdio.h> int main() { ; printf("%#x\n",a); ; } /* output 0xf; */ //(15)10= ...
- 【bzoj3573】[HNOI2014]米特运输
题目描述 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市为首都.这N个城 ...