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. arpg网页游戏之地图(一)

    [转]http://www.cnblogs.com/BlueWoods/p/4681572.html?from=timeline&isappinstalled=1 开发arpg网页游戏一项比较 ...

  2. EmptyRecycle() 清空回收站

    //在uses下面引用 function SHEmptyRecycleBinA(Wnd:HWND;str:PChar;WRD:DWORD):Integer;stdcall; external 'SHe ...

  3. Linux C学习笔记07--管道通信

    管道通信属于进程间通信的一种方式,使用方便,但是局限是父进程与子进程间的通信,下面是调试好的代码. 程序父进程创建2个管道,分别用于父进程写数据--子进程读数据和子进程写数据--父进程读数据: #in ...

  4. python 排序

    python 写的排序,实现起来还是比较简单 #快速排序 def qsort(L): if len(L)>1: return qsort([i for i in L[1:] if i<L[ ...

  5. VC MFC在CMFCToolBar工具栏中加入组合框

    如何在CMFCToolBar工具栏中加入组合框等控件,且先看在线MSDN上怎么说的: 要增加一个组合框,需要完成以下步骤: 1.在工具栏资源中,增加一个对应ID资源号的按钮. 2.在主框架(mainf ...

  6. 去掉DLL can move

    1.OptionalHeader.DllCharacteristics = wNewDllCharacteristics; 用CFF打开,如果存在DLL can move这个选项,去掉即可 2.Rel ...

  7. windows10-桌面图标不见了,资源管理器的桌面中可以看到??

    问题描述: 1. 桌面的图标,在桌面上看不到, 但是在通过资源管理器可以看到, 图标仍然在桌面 2. 桌面仍然可以右击, 就是看不见新建或者拷贝到桌面的所有图标 解决方案: Google 后请参考: ...

  8. textview 多行 省略号

    TextView自带的可以通过 android:ellipsize="end" android:singleLine="true"实现单行省略,  多行显示: ...

  9. [wechall] Time to Reset (Exploit, Coding, PHP)

    Time to Reset (Exploit, Coding, PHP) <?php $start=1453000000; $cstf="nipNlJ6ZQPuGQ3i90QUTP8J ...

  10. ffmpeg未整理好,有时间整理下

    v  容器(Container) v  容器就是一种文件(封装)格式,比如flv.mkv.ts.mp4.rmvb.avi等.包含下面5种流以及文件头信息. v  流(Stream) v  是一种视频数 ...