【剑指**】20.表示数值的字符串
2018-01-24 14:24
190 查看
20.表示数值的字符串
题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符 串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但 是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
题目分析
详细分析请参考 《剑指OFFER》第二版 p127这道题目非常经典,因为它理解起来足够简单,但是程序实现上却很考查编程技巧,如果直接的根据条件去书写代码,会让代码越来越庞大;
针对自己对指针的不熟练,这道题目也可以作为自己熟悉指针,和字符串操作练习的好题目;
代码
class Solution { public: bool isNumeric(char* string) { if (string == NULL) return false; bool numeric = scanInteger(&string); if (*string == '.') { ++string; numeric = scanUnsignedInteger(&string) || numeric; } if (*string == 'e' || *string == 'E') { ++string; numeric = numeric && scanInteger(&string); } return numeric && *string == '\0'; } bool scanInteger(char** ch) { if (**ch == '+' || **ch == '-') { ++(*ch); } return scanUnsignedInteger(ch); } bool scanUnsignedInteger(char** ch) { const char * before = *ch; while (**ch != '\0' && **ch >= '0' && **ch <= '9' ) { ++(*ch); } return *ch > before; } };
相关文章推荐
- 剑指offer--面试题20:表示数值的字符串
- 剑指Offer 面试题20:表示数值的字符串 Java代码实现
- 【剑指offer】面试题20:表示数值的字符串
- 剑指Offer-20:表示数值的字符串
- 剑指20:表示数值的字符串
- 剑指offer--表示数值的字符串
- 《剑指offer》面试题20:表示数值的字符串
- 剑指offer-表示数值的字符串-java
- 剑指Offer面试题54:表示数值的字符串
- 剑指offer-表示数值的字符串
- 剑指offer-面试题54-表示数值的字符串
- 剑指offer-----判断字符串是否表示数值(java版)
- 剑指Offer:表示数值的字符串
- 剑指offer--表示数值的字符串
- 【剑指offer——JAVA实现】表示数值的字符串(含思路解答示意图)
- 剑指offer:表示数值的字符串
- 剑指offer-53.表示数值的字符串
- 剑指offer(50)-表示数值的字符串
- 剑指Offer(20)表示数值的字符串
- 剑指offer——面试题54:表示数值的字符串