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 ...
随机推荐
- ADT Bundle下载和安装
下载官方adt集成包(即ADT Bundle)并安装. Android官方已经推出adt集成包,包含了eclipse.sdk和SDK Manager,只需解压出来,然后就能运行Eclipse. 官方集 ...
- return false break;
js中的return false; break; , , , , ]; var list2 = ['a', 'b', 'c', 'd']; ; j < list2.length; j++) { ...
- Open Source Log Management
https://www.elastic.co/solutions/logging The Elastic Stack (sometimes known as the ELK Stack) is the ...
- [JavaScript] - form表单转json的插件
jquery.serializejson.js 之前好像记录过,做项目又用到了再记下 在页面中引入js后就可以使用了 示例: //点击设置微信信息的form表单提交按钮后,执行wxConfig的con ...
- stm32 ADC使用方法
void Adc_Init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AP ...
- 软件测试实习生 带人计划 Plan for Training Inten
临时拟了个提纲,以后慢慢补充吧 序号 培训内容 时间安排 1 根据项目需求,编写测试用例,针对存储过程 2 存储过程的走读,以及怎样执行测试用例和查看结果 3 根据项目需求,编写测试用例,针对接口[C ...
- django URL路由基础
URL是Web服务的入口,用户通过浏览器发送过来的任何请求,都是发送到一个指定的URL地址,然后被响应. 在Django项目中编写路由,就是向外暴露我们接收哪些URL的请求,除此之外的任何URL都不被 ...
- Bate冲刺四——《WAP团队》
β冲刺第四天 1. 今日完成任务情况以及遇到的问题. ①马麒.杜有海:记录功能完善情况 ②郝明宇:记录验收情况 ③马宏伟.周欣:后台前端数据连接 ④乌勒扎:综合测试 2.成员时间贡献 成员 马宏 ...
- zookeeper篇-如何修改源码
提一个问题先 zxid有64位,分成两部分: 高32位是Leader的epoch:选举时钟,每次选出新的Leader,epoch累加1 低32位是在这轮epoch内的事务id:对于用户的每一次更新操作 ...
- 2018年全国多校算法寒假训练营练习比赛(第一场)C 六子冲
https://www.nowcoder.com/acm/contest/67/C 思路: 模拟. 代码: #include<bits/stdc++.h> using namespace ...