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的更多相关文章

  1. LeetCode OJ:Valid Sudoku(有效数独问题)

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  2. LeetCode OJ:Ugly Number II(丑数II)

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  3. LeetCode OJ:Valid Parentheses(有效括号)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. 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 ...

  5. LeetCode OJ:Valid Palindrome(验证回文)

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  6. LeetCode OJ:Happy Number(欢乐数)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  8. LeetCode OJ:Largest Number(最大数字)

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. LeetCode OJ:Missing Number (丢失的数)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

随机推荐

  1. ubuntu下交叉编译mono

    环境:ubuntu16.04 wget download.mono-project.com/sources/mono/mono-4.8.1.0.tar.bz2 配置: CC=arm-linux-you ...

  2. mysql数据库分库分表(Sharding)

    mysql数据库切分 前言 通过MySQLReplication功能所实现的扩展总是会受到数据库大小的限制.一旦数据库过于庞大,尤其是当写入过于频繁,非常难由一台主机支撑的时候,我们还是会面临到扩展瓶 ...

  3. 51NOD 1027 大数乘法

    1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B (A,B ...

  4. BFS-迷宫问题-用宽度(广度)优先搜索解决最优路径问题

    题目: 给定一个大小为 N×M 的迷宫.迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格 的通道移动.请求出从起点到终点所需的最小步数.请注意,本题假定从起点一定可以移动 到终点. 限制条件;N, ...

  5. python 线性查找

    import random val= data=[,,,,] : find= val=int(input('请输入查找键值(1-9),输入-1离开:')) for i in data: if i==v ...

  6. ubuntu16.04 kinetic 安装 robot-pose-publisher

    sudo apt-get install ros-kinetic-robot-pose-publisher

  7. SQLServer 中的SYSPROCESSES表

    性能测试的时候对SQLserver DB中的CPU,io,内存进行监控 可以通过系统进程表查看资源使用情况 注意要有master权限 select top 10 * from SYSPROCESSES ...

  8. root登录不进去 dropbear ssh

    安装好了dropbear, root 怎么也登录不进去. 看 /var/log/messages , 发觉有很多下面的消息, 网上查了一下, 发觉建个 /etc/shells 文件,然后把 /bin/ ...

  9. Java数组的定义和使用

    如果希望保存一组有相同类型的数据,可以使用数组. 数组的定义和内存分配 Java 中定义数组的语法有两种: type arrayName[]; type[] arrayName; type 为Java ...

  10. byte[]数组与十六进制字符串与字符串的互相转换 ——转载

    https://www.cnblogs.com/lelehellow/p/6369631.html