剑指 Offer 20. 表示数值的字符串
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100"、“5e2”、"-123"、“3.1416”、"-1E-16"、“0123"都表示数值,但"12e”、“1a3.14”、“1.2.3”、"±5"及"12e+5.4"都不是。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解析
题目的目的就是要遍历字符串,判断字符串是否满足一个数值要求,比如一个数值里不会存在两个小数点,而且也有其他要求,我们只要一个一个遍历,判断当前字符是否满足上一个字符的后接要求,比如+,-后面跟的是数字,肯定不能跟随+,-,所以读取一个字符,判断它下一个字符应该是什么,当读取下一个字符的时候,是否满足要求,不满足要求就返回错误。然后再判断初始状态,结束状态即可。具体可以参考原文
代码
class Solution {
public:
enum State
{
STATE_INITIAL
,
STATE_INT_SIGN
,
STATE_INTEGER
,
STATE_POINT
,
STATE_POINT_WITHOUT_INT
,
STATE_FRACTION
,
STATE_EXP
,
STATE_EXP_SIGN
,
STATE_EXP_NUMBER
,
STATE_END
,
};
enum CharType
{
CHAR_NUMBER
,
CHAR_EXP
,
CHAR_POINT
,
CHAR_SIGN
,
CHAR_SPACE
,
CHAR_ILLEGAL
,
};
CharType
toCharType(char ch
) {
if (ch
>= '0' && ch
<= '9') {
return CHAR_NUMBER
;
} else if (ch
== 'e' || ch
== 'E') {
return CHAR_EXP
;
} else if (ch
== '.') {
return CHAR_POINT
;
} else if (ch
== '+' || ch
== '-') {
return CHAR_SIGN
;
} else if (ch
== ' ') {
return CHAR_SPACE
;
} else {
return CHAR_ILLEGAL
;
}
}
bool isNumber(string s
) {
unordered_map
<State
, unordered_map
<CharType
, State
>> transfer
{
{
STATE_INITIAL
, {
{CHAR_SPACE
, STATE_INITIAL
},
{CHAR_NUMBER
, STATE_INTEGER
},
{CHAR_POINT
, STATE_POINT_WITHOUT_INT
},
{CHAR_SIGN
, STATE_INT_SIGN
},
}
}, {
STATE_INT_SIGN
, {
{CHAR_NUMBER
, STATE_INTEGER
},
{CHAR_POINT
, STATE_POINT_WITHOUT_INT
},
}
}, {
STATE_INTEGER
, {
{CHAR_NUMBER
, STATE_INTEGER
},
{CHAR_EXP
, STATE_EXP
},
{CHAR_POINT
, STATE_POINT
},
{CHAR_SPACE
, STATE_END
},
}
}, {
STATE_POINT
, {
{CHAR_NUMBER
, STATE_FRACTION
},
{CHAR_EXP
, STATE_EXP
},
{CHAR_SPACE
, STATE_END
},
}
}, {
STATE_POINT_WITHOUT_INT
, {
{CHAR_NUMBER
, STATE_FRACTION
},
}
}, {
STATE_FRACTION
,
{
{CHAR_NUMBER
, STATE_FRACTION
},
{CHAR_EXP
, STATE_EXP
},
{CHAR_SPACE
, STATE_END
},
}
}, {
STATE_EXP
,
{
{CHAR_NUMBER
, STATE_EXP_NUMBER
},
{CHAR_SIGN
, STATE_EXP_SIGN
},
}
}, {
STATE_EXP_SIGN
, {
{CHAR_NUMBER
, STATE_EXP_NUMBER
},
}
}, {
STATE_EXP_NUMBER
, {
{CHAR_NUMBER
, STATE_EXP_NUMBER
},
{CHAR_SPACE
, STATE_END
},
}
}, {
STATE_END
, {
{CHAR_SPACE
, STATE_END
},
}
}
};
int len
= s
.length();
State st
= STATE_INITIAL
;
for (int i
= 0; i
< len
; i
++) {
CharType typ
= toCharType(s
[i
]);
if (transfer
[st
].find(typ
) == transfer
[st
].end()) {
return false;
} else {
st
= transfer
[st
][typ
];
}
}
return st
== STATE_INTEGER
|| st
== STATE_POINT
|| st
== STATE_FRACTION
|| st
== STATE_EXP_NUMBER
|| st
== STATE_END
;
}
};
作者:LeetCode
-Solution
链接:https
://leetcode
-cn
.com
/problems
/biao
-shi
-shu
-zhi
-de
-zi
-fu
-chuan
-lcof
/solution
/biao
-shi
-shu
-zhi
-de
-zi
-fu
-chuan
-by
-leetcode
-soluti
/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。