当前位置:网站首页>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
然后分情况判断
边栏推荐
- 如何评价代码质量
- Qualcomm 9x07 two startup modes
- 谈谈 @Autowired 的实现原理
- There are some limitations in cluster expansion and contraction
- The first day of employment more than ten years ago
- How to restore visualizations and dashboards after kibana rebuilds the index
- 【论文笔记】Catching Both Gray and Black Swans: Open-set Supervised Anomaly Detection*
- 2-用线段构成图形、坐标转换
- Arthas vmtool命令小结
- Android kotlin coroutines KTX extension
猜你喜欢

Object. Defineproperty() and data broker

Why use growth neural gas network (GNG)?

RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?

jmeter压测结果分析

给你的win10装一个wget

Object.defineProperty() 和 数据代理

实战监听Eureka client的缓存更新

点云库pcl从入门到精通 第十章

The first day of employment more than ten years ago

坑爹的“敬业福”:支付宝春晚红包技术大爆发
随机推荐
5-rotating Daisy - rotating canvas and timer
Map interface and its sub implementation classes
Pyspark on HPC (Continued): reasonable partition processing and consolidated output of a single file
Optimize your gradle module with a clean architecture
Map (set) operation in go language
jmeter压测结果分析
Lighthouse cloud desktop experience
驱动架构 & platform平台总线驱动模型
Dongyuhui, the "square face teacher", responded that the popularity was declining: do a good job of live broadcasting of agricultural products to benefit farmers and consider supporting education
6-shining laser application of calayer
How to use the template library of barcode label software
vector的深度剖析及模拟实现
[operating steps] how to set the easynvr hardware device to be powered on without automatic startup?
The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
[paper notes] catching both gray and black swans: open set supervised analog detection*
Monitor the cache update of Eureka client
坑爹的“敬业福”:支付宝春晚红包技术大爆发
Arthas vmtool命令小结
给你的win10装一个wget
438. Find All Anagrams in a String