[LeetCode] 65. 有效数字
题目链接 : https://leetcode-cn.com/problems/valid-number/
题目描述:
验证给定的字符串是否可以解释为十进制数字。
例如:
"0"` => `true`
`" 0.1 "` => `true`
`"abc"` => `false`
`"1 a"` => `false`
`"2e10"` => `true`
`" -90e3 "` => `true`
`" 1e"` => `false`
`"e3"` => `false`
`" 6e-1"` => `true`
`" 99e2.5 "` => `false`
`"53.5e93"` => `true`
`" --6 "` => `false`
`"-+3"` => `false`
`"95a54e53"` => `false
说明: 我们有意将问题陈述地比较模糊。在实现代码之前,你应当事先思考所有可能的情况。这里给出一份可能存在于有效十进制数字中的字符列表:
- 数字 0-9
- 指数 - "e"
- 正/负号 - "+"/"-"
- 小数点 - "."
当然,在输入中,这些字符的上下文也很重要。
思路:
思路一:作弊法
class Solution:
def isNumber(self, s: str) -> bool:
try:
num = float(s)
return True
except:
return False
思路二:考虑所有情况
思路三:有限自动机

以上思路二,三皆来自网络,我不是大佬,只是大自然搬运工!
代码:
思路二:
class Solution:
def isNumber(self, s: str):
s = s.strip()
#print(s)
dot_seen = False
e_seen = False
num_seen = False
for i, a in enumerate(s):
if a.isdigit():
num_seen = True
elif a == ".":
if e_seen or dot_seen:
return False
dot_seen = True
elif a == "e":
if e_seen or not num_seen:
return False
num_seen = False
e_seen = True
elif a in "+-":
if i > 0 and s[i - 1] != "e":
return False
else:
return False
return num_seen
思路三:
class Solution:
def isNumber(self, s: str) -> bool:
state = [
{},
# 状态1,初始状态(扫描通过的空格)
{"blank": 1, "sign": 2, "digit": 3, ".": 4},
# 状态2,发现符号位(后面跟数字或者小数点)
{"digit": 3, ".": 4},
# 状态3,数字(一直循环到非数字)
{"digit": 3, ".": 5, "e": 6, "blank": 9},
# 状态4,小数点(后面只有紧接数字)
{"digit": 5},
# 状态5,小数点之后(后面只能为数字,e,或者以空格结束)
{"digit": 5, "e": 6, "blank": 9},
# 状态6,发现e(后面只能符号位, 和数字)
{"sign": 7, "digit": 8},
# 状态7,e之后(只能为数字)
{"digit": 8},
# 状态8,e之后的数字后面(只能为数字或者以空格结束)
{"digit": 8, "blank": 9},
# 状态9, 终止状态 (如果发现非空,就失败)
{"blank": 9}
]
cur_state = 1
for c in s:
if c.isdigit():
c = "digit"
elif c in " ":
c = "blank"
elif c in "+-":
c = "sign"
if c not in state[cur_state]:
return False
cur_state = state[cur_state][c]
if cur_state not in [3, 5, 8, 9]:
return False
return True
[LeetCode] 65. 有效数字的更多相关文章
- C#版 - Leetcode 65. 有效数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- Java实现 LeetCode 65 有效数字
65. 有效数字 验证给定的字符串是否可以解释为十进制数字. 例如: "0" => true " 0.1 " => true "abc&q ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- [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)-Power of Four
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- LeetCode (65):Same tree
Total Accepted: 83663 Total Submissions: 200541 Difficulty: Easy Given two binary trees, write a fun ...
随机推荐
- C#调用Python(一)
python文件中未引入其他包.模块 以下方法不适用于pyhton 文件有第三方包.模块,有第三方包,模块的实现方法,请戳这里→https://www.cnblogs.com/zhuanjiao/p/ ...
- python-套接字编程之tcp
服务端和客户端. 服务端脚本: # Auther:AlphaPanda # Description:server # Version:1 # Date:Mon Dec 2 09:02:01 EST 2 ...
- MySQL 运维管理平台
github: https://github.com/XiaohaoYu/mysql_platform
- php7魔术方法
抽空把php7的魔术方法复习一下,大致如下 __constract:在实例化一个类时,触发 __destruct:在一个实例对象被销毁的时候触发 __set(string $name , mixed ...
- 容器适配器————stack
只能访问 stack 顶部的元素:只有在移除 stack 顶部的元素后,才能访问下方的元素. 堆栈操作 top():返回一个栈顶元素的引用,类型为 T&.如果栈为空,返回值未定义. push( ...
- Windows 下安装Apache web服务器
1.Apache 服务器的下载 进入下载页面:http://httpd.apache.org/download.cgi 为提高下载速度,镜像选择清华大学的服务器(http://mirrors.tuna ...
- [BZOJ2839]:集合计数(组合数学+容斥)
题目传送门 题目描述 .(是质数喔~) 输入格式 一行两个整数N,K. 输出格式 一行为答案. 样例 样例输入: 3 2 样例输出: 样例说明 假设原集合为{A,B,C} 则满足条件的方案为:{AB, ...
- for aws associate exam
Topics which I read based on the previous forum discussions Amazon DynamoDB January 2016 Day at the ...
- 进程间通信(IPC)-管道、匿名管道
每个进程都有各自的地址空间,任何一个进程的全局变量在另一个进程中都看不到 所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把数据读 ...
- modern php笔记---2.1、特性(命名空间、特性、性状)
modern php笔记---2.1.特性(命名空间.特性.性状) 一.总结 一句话总结: legend2是真的非常好用,资质起码提升5倍,也就是学习效率提升了起码5倍 1.命名空间实质? 从技术层面 ...