当前位置:网站首页>65. Valid Number
65. Valid Number
2022-06-23 08:24:00 【ujn20161222】
A valid number can be split up into these components (in order):
- A decimal number or an integer.
- (Optional) An
'e'or'E', followed by an integer.
A decimal number can be split up into these components (in order):
- (Optional) A sign character (either
'+'or'-'). - One of the following formats:
- One or more digits, followed by a dot
'.'. - One or more digits, followed by a dot
'.', followed by one or more digits. - A dot
'.', followed by one or more digits.
- One or more digits, followed by a dot
An integer can be split up into these components (in order):
- (Optional) A sign character (either
'+'or'-'). - One or more digits.
For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"].
Given a string s, return true if s is a valid number.
Example 1:
Input: s = "0" Output: true
Example 2:
Input: s = "e" Output: false
Example 3:
Input: s = "." Output: false
Constraints:
1 <= s.length <= 20sconsists of only English letters (both uppercase and lowercase), digits (0-9), plus'+', minus'-', or dot'.'.
Accepted
263.2K
Submissions
1.5M
class Solution:
def isNumber(self, s: str) -> bool:
def is_integer(s):
return s.isdigit() or len(s)>0 and s[0] in "+-" and s[1:].isdigit()
def is_decimal(s):
parts=s.split(".")
if len(parts)!=2:return False
if is_integer(parts[0]) and parts[1].isdigit():return True
if parts[0] in ["-","+",""] and parts[1].isdigit():return True
if is_integer(parts[0]) and not parts[1]:return True
s=s.lower()
parts=s.split("e")
if len(parts)>2:return False
if not is_integer(parts[0]) and not is_decimal(parts[0]):return False
return True if len(parts)==1 else is_integer(parts[1])
写两个函数is_integer is_decimal
然后分情况判断
边栏推荐
- Go data types (II) overview of data types supported by go and Boolean types
- Leetcode topic analysis spiral matrix II
- Monitor the cache update of Eureka client
- Leetcode topic analysis sort colors
- Top 25 most popular articles on vivo Internet technology in 2021
- 十多年前的入职第一天
- Arthas vmtool命令小结
- Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
- How to restore visualizations and dashboards after kibana rebuilds the index
- 4-绘制椭圆、使用定时器
猜你喜欢

Linux Mysql安装
![[cloud computing] GFS ideological advantages and architecture](/img/98/2a4ef0ca805add24d431dac9808903.png)
[cloud computing] GFS ideological advantages and architecture

十多年前的入职第一天

3. Caller 服务调用 - dapr

Point cloud library PCL from introduction to mastery Chapter 10

PCB电路板特性检查项目都有哪些?

What are the PCB characteristics inspection items?

Self organizing map neural network (SOM)

Install a WGet for your win10

高通9x07两种启动模式
随机推荐
Easycvr accesses the website through the domain name. How to solve the problem that the video cannot be viewed back?
9 ways in which network security may change in 2022
Lightweight UI control library worth collecting
走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?
Hongmeng reads the resource file
How to restore visualizations and dashboards after kibana rebuilds the index
“方脸老师”董宇辉再回应热度下降:把农产品直播做好让农民受益 考虑去支教
Kernel log debugging method
Keng dad's "dedication blessing": red packet technology explosion in Alipay Spring Festival Gala
实战监听Eureka client的缓存更新
RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?
ArcLayoutView: 一个弧形布局的实现
C Advanced Learning -- extended method (this)
Focus! Ten minutes to master Newton convex optimization
2-用线段构成图形、坐标转换
Go language basic conditional statement if
Linux Mysql安装
[paper notes] catching both gray and black swans: open set supervised analog detection*
6-闪耀的激光-CALayer 的应用
Chapter 1 open LDAP master-slave synchronization tower construction