[LeetCode OJ]Valid Number
来源:互联网 发布:Java:什么是方法重写 编辑:程序博客网 时间:2024/06/05 15:53
Valid Number
我的思路:
1、这题是判断一个字符串是否是一个数字。这题在剑指Offer上有,我这边的代码主体是参考了那上面的,针对LeetCode的另一些特殊情况,有修改。
2、思路如下:如果是空字符串,返回否。接着,跳过前面的空格,如果当前字符是正负号,那么下一个,如果结束了,就返回错误,没有就去扫描数字的长度,接着扫描空格的长度,如果没有空格,而且没有结束,判断是否是“.”号,如果是接下来,判断是否有空格,如果之前有数字而且已经结束,就返回true;思路没怎么整理。也是随便改,根据出现了什么情况再修改代码。
3、下面是一些特殊情况:不是数字:0. 1;0;.e1;1 .;0e;.;..; ;是数字的情况:1;2.;.1;46.e3;2e0。
代码如下:
int scanDigits(char** string) { int i = 0; while (**string != '\0' && **string <='9' && **string >= '0') { ++(*string); i++; } return i;}int scanSpace(char** string) { int i = 0; while (**string != '\0' && **string == ' ') { ++(*string); i++; } return i;}bool isExponential(char **string) { if (**string != 'e' && **string != 'E') return false; ++(*string); if (**string == '-' || **string == '+') ++(*string); if (**string == '\0') return false; int numNumber = scanDigits(string); scanSpace(string); return (numNumber && (**string == '\0')) ? true : false;}bool isNumber(char *s) { if (s == NULL) return false; scanSpace(&s); if (*s == '+' || *s == '-') s++; if (*s == '\0') return false; int oldnumNumber = scanDigits(&s); int numSpace = scanSpace(&s); if (!numSpace && (*s != '\0')) { if (*s == '.') { ++s; numSpace = scanSpace(&s); if (oldnumNumber && (*s == '\0')) return true; int numNumber = scanDigits(&s); if (numSpace && numNumber) return false; if (!numSpace && (oldnumNumber || numNumber) && (*s == 'e' || *s == 'E')) return isExponential(&s); else { scanSpace(&s); return (numNumber && (*s == '\0')); } } else if (oldnumNumber && (*s == 'E' || *s == 'e')) return isExponential(&s); else return false; } return (*s == '\0');}
0 0
- LeetCode OJ:Valid Number
- LeetCode OJ - Valid Number
- LeetCode OJ Valid Number
- [LeetCode OJ]Valid Number
- http://oj.leetcode.com/problems/valid-number/
- LeetCode OJ 65 Valid Number[hard]
- LeetCode OJ算法题(六十六):Valid Number
- LeetCode OJ 之 Valid Number (有效数字的判断)
- LeetCode OJ 系列之65 Valid Number --Python
- 【LeetCode OJ】Valid Parentheses
- LeetCode OJ:Valid Palindrome
- LeetCode OJ:Valid Sudoku
- LeetCode OJ:Valid Parentheses
- LeetCode OJ - Valid Palindrome
- LeetCode OJ - Valid Parentheses
- LeetCode OJ - Valid Sudoku
- LeetCode OJ Valid Sudoku
- LeetCode OJ Valid Palindrome
- Volley_Get_Post
- 批处理设置静态、动态ip
- ConcurrentHashMap 介绍
- SpringMVC中使用Interceptor拦截器(仅作笔记学习)
- 如何看论文,找论文,写作,及处理情感因素
- [LeetCode OJ]Valid Number
- Unity导出ios工程如何减小运行内存、安装包(ipa)、以及安装后大小
- stm32 EXTI中断BUG,无法进入外部中断的问题
- 程序员修炼之路-(3)排序(上):基本排序
- Solaris 10下使用Python3
- 设计模式(java)——简单工厂模式
- swing Jable 学习(一)—TableColumnModel
- Intersection of Two Linked Lists
- 工作5年之后,对java基础的一点回忆 (1)