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 ...
 
随机推荐
- 打开vi后提示The ycmd server SHUT DOWN (restart with :YcmRestartServer)该如何处理
			
答:进入YouCompleteMe的安装目录安装一些必要的依赖 比如:笔者将YouCompleteMe安装到了~/.vim/bundle目录下,那么执行以下操作: cd ~/.vim/bundle/Y ...
 - luogu P3387 【模板】缩点
			
题目 好久没法博客了 这次就水个板子题目吧 tarjan缩点之后重新建图 而且边权应该都是正的(要不我怎么能这么轻松水过去) 在新图上记忆化一下就好了 f[i] 表示 开头选i这个点 的 路径最大值 ...
 - perl入门知识(2)
			
交互式编程你可以在命令行中使用 -e 选项来输入语句来执行代码,实例如下:$ perl -e 'print "Hello World\n"'输入以上命令,回车后,输出结果为:Hel ...
 - 论文笔记——ThiNet: A Filter Level Pruning Method for Deep Neural Network Compreesion
			
论文地址:https://arxiv.org/abs/1707.06342 主要思想 选择一个channel的子集,然后让通过样本以后得到的误差最小(最小二乘),将裁剪问题转换成了优化问题. 这篇论文 ...
 - 推荐一个SAM文件中flag含义解释工具--转载
			
SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...
 - selenium-chrome-headless
			
#coding=utf-8 from selenium import webdriver import time chrome_options = webdriver.ChromeOptions() ...
 - c语言 快速排序
			
#include<stdio.h> #include<stdlib.h> #define BUF_SIZE 10 void display(int array[], int m ...
 - Topless eclipse导入myeclipse的web项目没法识别问题解决
			
1.进入项目目录,找到.project文件,打开. 2.找到<natures>...</natures>代码段. 3.在第2步的代码段中加入如下标签内容并保存: <nat ...
 - LeetCode--119--杨辉三角II
			
问题描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你 ...
 - memcached-1.4.20 主要启动流程笔记
			
以下笔记主要是关注tcp模式下memcached的启动过程. main() 设置信号处理函数为sig_handler() 初始化系统设置,保存在全局变量settings里面 settings_init ...