【Leetcode】【Hard】Valid Number
Validate if a given string is numeric.
Some examples:"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
本题需要考虑:
1、字符前的空格
2、字符正负号
3、检查是否是数字,数字中可以包含一个‘.’小数点,数字至少应存在一位
4、略过数字和点后,检查是否有‘e’,如果有:
(1)检查指数是否有正负
(2)检查其后是否有数字,数字至少存在一位
5、字符后的空格
6、最后遇到'\0'则返回true,否则false
代码:
class Solution {
public:
bool isNumber(string s) {
int i = ; // skip the whilespaces
for(; s[i] == ' '; i++) {} // check the significand
if(s[i] == '+' || s[i] == '-') i++; // skip the sign if exist int n_nm, n_pt;
for(n_nm=, n_pt=; (s[i]<='' && s[i]>='') || s[i]=='.'; i++)
s[i] == '.' ? n_pt++:n_nm++;
if(n_pt> || n_nm<) // no more than one point, at least one digit
return false; // check the exponent if exist
if(s[i] == 'e') {
i++;
if(s[i] == '+' || s[i] == '-') i++; // skip the sign int n_nm = ;
for(; s[i]>='' && s[i]<=''; i++, n_nm++) {}
if(n_nm<)
return false;
} // skip the trailing whitespaces
for(; s[i] == ' '; i++) {} return s[i]==; // must reach the ending 0 of the string
}
};
【Leetcode】【Hard】Valid Number的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Using AFNetWorking 2.0 upload file to php web service server based on Slim
Recently i am developing the IOS app, a feature is needed to upload image to the webservice server. ...
- php关闭错误提示
今天调试phalcon的一个接口时候碰到如下提示: Deprecated: mongogo::mongogo(): The Mongo class is deprecated, please use ...
- make: g77: Command not found
编译cblas时报错,这时,修改Makefile.in中的编译文件中的g77为gfortran
- avalon2的后端渲染实践
avalon2为了提高性能,采用全新的架构,四层架构,其中一层为虚拟DOM. 虚拟DOM的一个好处是能大大提高性能,另一个好处是能过错整描述我们的页面结构.因此在非浏览器环境下,虚拟DOM也能正常运行 ...
- Hibernate的配置文件以及用法
一. 三大框架 Hibernate 1.安装hibernate插件至ecilpse 2.进行配置 2.1 主配置文件 <?xml version="1.0" encoding ...
- VC++ 在两个程序中 传递字符串等常量值的方法:使用了 WM_COPYDATA 消息的
消息作用: 在进程间共享数据(内部通过创建内存映射文件) 消息介绍:需要用到的数据结构/类型:typedef struct tagCOPYDATASTRUCT { ULONG_PTR dw ...
- HTML速查列表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- LeetCode OJ 274. H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...
- Java多线程断点下载
public static class DownloadThread extends Thread{ private int threadId; private int startIndex; pri ...
- linux 系统下,如何清空文件内容
最近用 crontab命令 做了一些 同步任务,可后期同步任务的代码没用了,于是,我就直接删除了 代码(对应的jar包),但是 crontab -e 的命令行没有删除 ,以后,每天我的Azure vm ...