分享免费的编程资源和教程

网站首页 > 技术教程 正文

LeetCode 65. Valid Number

goqiw 2024-09-24 16:05:25 技术教程 58 ℃ 0 评论

Description

Validate if a given string can be interpreted as a decimal number.

Some examples:

"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

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

Numbers 0-9

Exponent - "e"

Positive/negative sign - "+"/"-"

Decimal point - "."

Of course, the context of these characters also matters in the input.

描述

验证给定字符串是否可以解释为十进制数。

思路

  • 这道题目考察的主要是各种情况的判断,本算算法考察的并不多,因此在LeetCode上得到的"Dislike"也非常多,这里说一下关键的点
  • 如果输入的字符串为空,则返回False
  • 如果只输入了一个字符,则必须是数字,否则直接返回False
  • 运算符"-","+"不能连续出现
  • 幂运算符"e"前后必须有数字(不一定需要直接紧挨),且只允许出现一次
  • "."只能出现一次,且必须有数字出现
  • 这道题考察点不是很清晰,做题时可以跳过

源代码文件在这里:https://github.com/ruicore/Algorithm/blob/master/Leetcode/2018-12-18-65-Valid-Number.py.

?本文首发于何睿的博客:https://www.ruicore.cn/leetcode-65-valid-number/,欢迎转载,转载需保留文章来源,作者信息和本声明.

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表