65. Valid Number *HARD*
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.
bool isNumber(string s) {
int l = s.length(), i;
bool flag=, point[]={,}, num[]={,};
for(i=; s[i] == ' '; i++);
for(; i<l; i++)
{
if(s[i] == ' ')
break;
if(s[i] == '-' || s[i] == '+')
{
if(num[flag] == true || point[flag] == true)
return false;
}
else if(isdigit(s[i]))
num[flag] = true;
else if(s[i] == '.')
{
if(point[flag] || flag == true)
return false;
point[flag] = true;
}
else if(s[i] == 'e')
{
if(flag == )
return false;
if(num[] == false)
return false;
flag = ;
}
else
return false;
}
for(;i<l;i++)
{
if(s[i]!=' ')
return false;
}
if(!num[] && !num[])
return false;
if(flag && !num[])
return false;
return true;
}
测试用例:
"0e" -- false
". 1" -- false
"-1." -- true
".-4" -- false
"+.8" -- true
"6e6.5" -- false
65. Valid Number *HARD*的更多相关文章
- [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 ...
- 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 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- 65. Valid Number
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- 65. Valid Number 判断字符串是不是数字
[抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...
随机推荐
- MySQL笔记(四)DDL与DML风格参考
便于 COPY ▲ 在所有操作之前: SET character_set_database=utf8; 确保 ↓ mysql> SHOW VARIABLES LIKE "%char%& ...
- spring MVC @Resource不支持Lazy加载及解决方法
今天迁一系统时发现有个bean使用@Resource注入了另外一个bean,这个被注入的bean是将被deprecated的类,而且只有一两个功能使用到,为了先调整进行测试,增加了@Lazy注解,启动 ...
- python函数总结
1.函数是一种子程序.程序员使用函数来减少代码重复,并用于组织或模块化程序.一旦定义了函数,它可以从程序中的许多不同位置被多次调用.参数允许函数具有可更改的部分.函数定义中出现的参数称之为形参,函数调 ...
- C++写入mbr
#include <windows.h> #include <winioctl.h> unsigned char scode[] = "\xb8\x12\x00\xc ...
- [luogu 2458][SDOI2006]保安站岗
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
- C# 如何调用启动窗体
Program.cs中代码如下: using System; using System.Collections.Generic; using System.Windows.Forms; namespa ...
- booststrap select2的应用总结
本身对前端js了解不是特别多,在项目中,遇到很多前端的问题,有时间整理一下,有不对的地方,不吝赐教,多多批评指正. 在项目中,遇到最多的select下拉框情景,莫过于多选和单选了 单选是很容易理解的, ...
- 为什么我的电脑win系统没有便笺功能?为什么我在开始菜单里找不到便笺功能?
有些网友表示,为什么我的电脑没有便笺功能?为什么我在开始菜单里找不到便笺功能? 从问题可以基本判断出来,这些网友使用的Win7版本有可能是买笔记本或者台式电脑时预装的Win7家庭普通版或者Win7精简 ...
- 使Apache支持PHP
1.用记事本打开Apache安装目录下conf文件夹中的httpd.conf文件,找到LoadModule部分的配置代码,在该部分添加下面的代码,将PHP模块加载到Apache服务中,使得Apache ...
- Java jdbc访问sqlserver,oracle数据库 DEMO
1.JDBC访问Oracle数据库 public class Jdbc_Oracle { // 静态代码块,只会执行一次,类似C#静态构造方法 static { try { // 加载数据库驱动一次 ...