[抄题]:
Validate if a given string is numeric.
Some examples:"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- +-号要求处于第一位,则index i = 0,不是它自己等于0
- 单个字符'e'要用单引号扩起来
[二刷]:
- s = s.trim();必须要有等号,否则空格害事
- 出错的情况直接return false, 不要设置变量,否则可能会拧回来
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
上面那张表
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution { public boolean isNumber(String s) { //trim first s = s.trim(); //ini some flags boolean numberSeen = false; boolean pointSeen = false; boolean eSeen = false; //for loop: num, . , e, +- for (int i = 0; i < s.length(); i++) { if (s.charAt(i) >= '0' && s.charAt(i) <= '9') { numberSeen = true; }else if (s.charAt(i) == '.') { if (pointSeen == true || eSeen == true) { return false; } pointSeen = true; }else if (s.charAt(i) == 'e') { if (pointSeen == true || eSeen == true || !(i >= 1 && s.charAt(i - 1) >= '0' && s.charAt(i - 1) <= '9')) { return false; } eSeen = true; numberSeen = false; }else if (s.charAt(i) == '+' || s.charAt(i) == '-') { if (i != 0 && s.charAt(i - 1) != 'e') return false; numberSeen = false; }else { //directly return return false; } } return numberSeen; } }