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

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【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 ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  6. 【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 ...

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

  8. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【LeetCode题意分析&解答】36. Valid Sudoku

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

  10. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. 【228】◀▶ Excel 函数说明

    官方帮助:Excel 函数(按字母顺序列出) 官方帮助:Excel 函数(按类别列出) 参考:EXCEL常用函数大全 01   N_ELEMENTS 表达式或者变量的元素个数. 02   DEFSYS ...

  2. XproerIM V1,2,12,65376 发布。

    客户端下载:http://yunpan.cn/QTCxKvcpC4Iet  访问密码 9141 更新说明:1.增加表情功能. 更新代码截图:

  3. 使用网易ubuntu镜像加速软件包安装

    用vi工具编辑文件 sudo vi /etc/apt/sources.list 以trusty(14.04)为例,插入下面内容到文件的最前面: deb http://mirrors.163.com/u ...

  4. 自动获取socket链接状态

    C# TcpClient在连接成功后无法检测连接状态,即使对方关闭了网络连接.以下扩展可检测连接状态: static class TcpClientEx { public static bool Is ...

  5. python-操作MySQL数据库

    1.安装MySQLdb? 为了用DB-API编写MySQL脚本,必须确保已经安装了MySQL.复制以下代码,并执行: #!/usr/bin/python # -*- coding: UTF-8 -*- ...

  6. HttpURLConnection 下载文件

    String fileUrl = request.getRequestURL().substring(0, request.getRequestURL().indexOf("/rest&qu ...

  7. iOS button文字居中

    新建一个UIButton的category .h @interface UIButton (QXTitleInCenter) -(instancetype)init; @end .m @impleme ...

  8. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  9. DataTable以列分组

    //DataTable以列分组 var result = from r in dt.AsEnumerable() group r by ), b = r.Field<) } into g sel ...

  10. Android--多线程之Handler(转)

    前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不 允许Activity新启动的线程访问该Activity里的 ...