[LeetCode] 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.
Update (2014-12-06):
New test cases had been added. Thanks unfounder's contribution.
确认输入的字符串是否为一个数值,一系列的判断,主要是一些位置的判断:
- 输入前置空格
- 正负号
- 连续的数值,包括‘.’
- 符号e
- 正负号
- 连续数值,不包括'.'
- 后续空格
按上面的规则便行。
#include <iostream>
using namespace std; class Solution {
public:
bool isNumber(const char *s)
{
int idx =;
for(;s[idx]==' ';idx++);
if(s[idx]=='-'||s[idx]=='+') idx++;
int Point=,Num=;
for(;(s[idx]>=''&&s[idx]<='')||s[idx]=='.';idx++)
s[idx]=='.'?Point++:Num++;
if(Point>||Num<) return false;
if(s[idx]=='e'){
idx++;
if(s[idx]=='-'||s[idx]=='+') idx++;
Num=;
for(;s[idx]>=''&&s[idx]<='';idx++) Num++;
if(Num<) return false;
}
for(;s[idx]==' ';idx++);
return s[idx]=='\0';
}
}; int main()
{
char a[]="-e-";
Solution sol;
cout<<sol.isNumber(a)<<endl;
return ;
}
[LeetCode] Valid Number 确认是否为数值的更多相关文章
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [leetcode]Valid Number @ Python
原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...
- LeetCode——Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- Leetcode Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- LeetCode Valid Number 有效数字(有限自动机)
题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...
- leetcode - valid number 正则表达式解法
import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
随机推荐
- django1.11文档 模型重点笔记
模型最重要的属性是Manager. 它是Django 模型进行数据库查询操作的接口,并用于从数据库提取实例. 如果没有自定义Manager,则默认的名称为objects. Managers 只能通过模 ...
- django之模型层
1. ORM MVC或者MTV框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...
- 动态规划、记忆化搜索:HDU1978-How many ways
Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标 ...
- 按时按登录IP记录Linux所有用户操作日志的方法(附脚本)
PS:Linux用户操作记录一般通过命令history来查看历史记录,但是如果因为某人误操作了删除了重要的数据,这种情况下history命令就不会有什么作用了.以下方法可以实现通过记录登陆IP地址和所 ...
- WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...
- P2255 [USACO14JAN]记录奥林比克Recording the M…
P2255 [USACO14JAN]记录奥林比克Recording the M… 题目描述 Being a fan of all cold-weather sports (especially tho ...
- OpenResty入门
写一个小例子--输出随机字符串 编写nginx配置文件 location /random { content_by_lua_file /usr/local/openresty/nginx/conf/l ...
- 有关js的一些小问题
忘了从哪里找来抄下来的: js执行顺序问题 1.函数的声明和调用 “定义式”函数声明 function fn1() {......} "赋值式"函数声明 var f=functio ...
- idea 安装findBugs 可以做代码扫描,也可以导出扫描结果生成扫描报告
idea 安装findBugs 可以做代码扫描,也可以导出扫描结果生成扫描报告 https://my.oschina.net/viakiba/blog/1838296 https://www.cnbl ...
- 如何将int转换为datetime?
$timestamp = 1210003200; $datetime = date('Y-m-d H:i:s', $timestamp); echo "该时间戳代表的时间:", $ ...