当前位置:网站首页>65. Valid Number
65. Valid Number
2022-06-23 08:47: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])
Write two functions is_integer is_decimal
Then judge according to the situation
边栏推荐
- 986. Interval List Intersections
- GeoServer adding mongodb data source
- Flutter achieves the effect of selecting seats in the cinema!
- 3-ProgressBar和二次裁剪
- 297. Serialize and Deserialize Binary Tree
- The first day of employment more than ten years ago
- Deep analysis and Simulation of vector
- Leetcode topic analysis spiral matrix II
- 最常用的5中流ETL模式
- Tencent cloud arm server evaluation practice
猜你喜欢

Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent

Summary of communication mode and detailed explanation of I2C drive

173. Binary Search Tree Iterator

3. Caller 服务调用 - dapr

论文阅读【Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset】

Analysis of JMeter pressure measurement results

The first day of employment more than ten years ago

Why do we say that the data service API is the standard configuration of the data midrange?

Multi-scale feature combination in target detection

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
随机推荐
Batch generation of code128- C barcode
3. caller service call - dapr
Leetcode topic analysis count primes
Driver Architecture & platform platform bus driver model
Only 187 bytes of desktop dream code
Which one is better for rendering renderings? 2022 latest measured data (IV)
5-rotating Daisy - rotating canvas and timer
Derivation and loading of the trained random forest model
Leetcode topic analysis set matrix zeroes
3-progressbar and secondary cropping
Spirit matrix for leetcode topic analysis
What is a dedicated server line
Kernel log debugging method
4- draw ellipse, use timer
词性家族
Go data types (II) overview of data types supported by go and Boolean types
6-shining laser application of calayer
Happy number of leetcode topic analysis
Summary of communication mode and detailed explanation of I2C drive
自组织映射神经网络(SOM)