剑指 Offer 20. 表示数值的字符串【字符串】

题目描述

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串”+100”、”5e2”、”-123”、”3.1416”、”-1E-16”、”0123”都表示数值,但”12e”、”1a3.14”、”1.2.3”、”+-5”及”12e+5.4”都不是。

解题思路

根据字符设置不同的状态,进行状态间的转换和判断。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static class Solution20 {
public boolean isNumber(String s) {
if (s.length() == 0) {
return false;
}
s = s.trim();//先去除字符串前后的空格
boolean numFlag = false;
boolean opsFlag = false;
boolean eFlag = false;
boolean dotFlag = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
numFlag = true;
} else if (c == '.' && !eFlag && !dotFlag) {//e后面不能有'.'
dotFlag = true;
} else if ((c == '+' || c == '-') && !opsFlag && (i == 0 || s.charAt(i - 1) == 'e' || s.charAt(i - 1) == 'E')) {
//只能有一个'+'/'-';获取前一个字符为'e'/'E';
opsFlag = true;
} else if ((c == 'e' || c == 'E') && !eFlag && numFlag) {//e前面至少要有一个数组出现。
eFlag = true;// 合格的数字只能有一个e
opsFlag = false;// 允许e后面有一个'+'/'-'
numFlag = false;// e后面一定要有 数字
} else {//其他情况都为非法数字形式。
return false;
}
}
return numFlag;
}
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1)

资源

https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/

剑指 Offer 20. 表示数值的字符串【字符串】

http://example.com/2021/04/16/20-表示数值的字符串/

Author

John Doe

Posted on

2021-04-16

Updated on

2021-06-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.