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 ...
随机推荐
- P3901 数列找不同
P3901 数列找不同 题目描述 现有数列 \(A_1,A_2,\cdots,A_N\) ,Q 个询问 \((L_i,R_i)\) , \(A_{Li} ,A_{Li+1},\cdots,A_{Ri} ...
- kubernetes 命令记录
操作基本命令: 通过yaml文件创建: kubectl create -f xxx.yaml (不建议使用,无法更新,必须先delete) kubectl apply -f xxx.yaml (创 ...
- C# 读取txt文件内容
if (!System.IO.File.Exists(@"E:\\111.txt")) { Console.Write("没有找到文件!"); } System ...
- jenkins 插件,下载地址
http://updates.jenkins-ci.org/download/plugins/ 通常我们需要下载的插件有如下几个:
- Jmeter工具做性能测试 常见的错误汇总
在Win机器上用Jmeter做性能测试,汇总下我自身遇到的错误和解决方案 java.net.BindException: Address already in use: JVM_Bind 原因分析:压 ...
- NIO 之 ByteBuffer
前言 对于刚接触ByteBuffer人来说,想要完全理解会稍微有点困难,正巧前几天有人问我,想到好久没写文章,就整理一下. 概念理解 对于ByteBuffer的一些概念不理解的情况下,如果直接打开源码 ...
- 监控视频采集与Web直播开发全流程分析
内容概要: 摄像头 => FFmpeg => Nginx服务器 => 浏览器 从摄像头拉取rtsp流 转码成rtmp流向推流服务器写入 利用html5播放 1.开发流程 1.1 通过 ...
- robot脚本编写规范
一个robot脚本主要有四部分组成: ***settings*** 设置 ***keywords*** 关键词 ***variables*** 变量 ***test cases*** 测试用例 一般, ...
- wpf窗口阴影
https://www.cnblogs.com/yiyan127/p/6362509.html
- linux使用flock文件锁
使用linux flock 文件锁实现任务锁定,解决冲突 格式: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command flock ...