LeetCode OJ:Valid Number
Validate if a given string is numeric.
Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true
基本上是leetCode上通过率最低的一道了,自己写了很多遍,就是有小问题通不过,最后参考了别人的写法,很精简,代码如下所示:
bool isNumber(const char * s)
{
int i = ;
int digitCount = ;
while(s[i] == ' ') i++; //skip spaces if(s[i]=='+' || s[i] == '-') i++; //skip sign while(isdigit(s[i])){
digitCount++;
i++;
} if(s[i] == '.') i++; while(isdigit(s[i])){
digitCount++;
i++;
} if(digitCount==) return false; if(s[i] == 'e' || s[i] == 'E'){
i++; if(s[i] == '+' || s[i] == '-') i++;//skp sign of expo if(!isdigit(s[i])) return false; while(isdigit(s[i])) i++;
} while(s[i] == ' ') i++; return s[i] == '\0';
}
这题比较特殊,使用c语言做是比较方便的,因为c字符串最后一位是'\0',即是前面通过 i 访问到最后一位的时候也能正常运行,但是使用java的话用上面的方法如果不注意就会出现outOfBound,c++却可以,也就是说c++字符串末尾的最后一位实际上也是可以访问的。
LeetCode OJ:Valid Number的更多相关文章
- LeetCode OJ:Valid Sudoku(有效数独问题)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode OJ:Ugly Number II(丑数II)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode OJ:Valid Parentheses(有效括号)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode OJ:Valid Anagram(有效字谜问题)
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- LeetCode OJ:Valid Palindrome(验证回文)
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- LeetCode OJ:Happy Number(欢乐数)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode OJ:Largest Number(最大数字)
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode OJ:Missing Number (丢失的数)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
随机推荐
- No compatible targets were found Do you wish to a add new Android Virtual Device ?
运行一个Android小程序时提示: No compatible targets were found Do you wish to a add new Android Virtual Device ...
- PyCharm/IDEA 使用技巧总结
基本概念 IDEA 没有类似 Eclipse 的工作空间的概念(workspace),最大单元就是 Project.这里可以把 Project 理解为 Eclipse 中的 workspace.Mod ...
- Python 开发环境搭建
Python分别有两个大的版本,分别是2和3 下载地址:Python-3.6.2 Python-2.7.13 现在安装路径:D:\Program Files\Python 安装完成以后要安装 pi ...
- POJ 3533 Light Switching Game(三维Nim积)题解
思路:三维Nim积 代码: #include<set> #include<map> #include<stack> #include<cmath> #i ...
- 【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)
brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...
- Leetcode ——Partition Equal Subset Sum
Question Given a non-empty array containing only positive integers, find if the array can be partiti ...
- java代码实现highchart与数据库数据结合完整案例分析(二)---折线图
作者原创:未经博主允许不许转载 在上一篇的博客中,展示和分析了如何做一个饼状图,有疑问可以参考上一篇博客. 现在分析和展示折线图的绘制和案例分析, 先展示效果图: 与饼状图不同的是,折线图展现更多的数 ...
- makefile 中的符号替换($@、$^、$<、$?)
Makefile $@, $^, $< $@ 表示目标文件$^ 表示所有的依赖文件$< 表示第一个依赖文件$? 表示比目标还要新的依赖文件列表 如一个目录下有如下文件: $ ls ...
- spring boot 捕获filter异常 统一返回处理结果
如前面的文章所述,controller中抛出的异常我们使用ControllerAdvice来处理: @RestControllerAdvice @Slf4j public class GlobalEx ...
- 关于PATH_INFO
nginx支持PATH_INFO? 想让nginx支持PATH_INFO,首先需要知道什么是pathinfo,为什么要用pathinfo? pathinfo不是nginx的功能,pathinfo是ph ...