【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 ...
随机推荐
- 如何分隔两个base64字符串?
如何分隔两个base64字符串? 用逗号或者任意的不在base64字符串内的字符都可以. All you have to do is to use a separator which is n ...
- 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法
问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...
- 通过Percona XtraBackup添加一个slave
假设我的master是172.16.15.8,要添加的slave是172.16.15.9 1:在主服务器执行备份操作(172.16.15.8) /usr/bin/innobackupex --comp ...
- checkbox绿色圆圈样式
抄自: http://www.cnblogs.com/xyzhanjiang/p/3989836.html?utm_source=tuicool&utm_medium=referral < ...
- css颜色大全-转载
FFFFFF #DDDDDD #AAAAAA #888888 #666666 #444444 #000000 #FFB7DD #FF88C2 #FF44AA #FF0088 #C10066 #A ...
- 一个很酷的加载loading效果--IT蓝豹
一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...
- X/Y型文案
[X/Y型文案] X型文案人,他们更像你语言学家.修辞学家和诗人,他们的日常工作就是想创意.查词典和构思修辞,以想办法用华丽的表达来描述产品. Y型文案往往并不华丽,有时甚至只不过是简单地描绘出用户心 ...
- bzoj 2152聪聪可可
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰 ...
- 一步一步写平衡二叉树(AVL树)
平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...
- openstackM版本常见问题汇总
汇总下常见的问题以及解释下一些比较容易让人萌的参数配置等等 问题汇总1.使用纯文本模式进行复制粘贴,打死不要用word!!!可以解决绝大多数问题,如果你依然执迷不悟,那么就好自为之吧 2.创建路由器时 ...