Validate if a given string is numeric.

Have you met this question in a real interview?

Yes
Example

"0" => true

" 0.1 " => true

"abc" => false

"1 a" => false

"2e10" => true

LeetCode上的原题,请参见我之前的博客Valid Number

class Solution {
public:
/**
* @param s the string that represents a number
* @return whether the string is a valid number
*/
bool isNumber(string& s) {
bool num = false, numAfterE = true, dot = false, exp = false, sign = false;
int n = s.size();
for (int i = ; i < n; ++i) {
if (s[i] == ' ') {
if (i < n - && s[i + ] != ' ' && (num || dot || exp || sign)) return false;
} else if (s[i] == '+' || s[i] == '-') {
if (i > && s[i - ] != 'e' && s[i - ] != ' ') return false;
sign = true;
} else if (s[i] >= '' && s[i] <= '') {
num = true;
numAfterE = true;
} else if (s[i] == '.') {
if (dot || exp) return false;
dot = true;
} else if (s[i] == 'e') {
if (exp || !num) return false;
exp = true;
numAfterE = false;
} else return false;
}
return num && numAfterE;
}
};

[LintCode] Valid Number 验证数字的更多相关文章

  1. [LeetCode] Valid Number 验证数字

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

  2. Valid Number 验证数字

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

  3. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  4. [LintCode] Valid Palindrome 验证回文字符串

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

  5. [LintCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [Swift]LeetCode65. 有效数字 | Valid Number

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

  7. [LeetCode] 125. Valid Palindrome 验证回文字符串

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

  8. [LeetCode] Valid Palindrome 验证回文字符串

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

  9. 【leetcode】Valid Number

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

随机推荐

  1. PMP 第二章 项目生命周期与组织

    1 项目组织机构类型有哪些? 区别是什么? 职能型  矩阵型  项目性 2 什么是事业环境因素? 什么是组织过程资产? 如何区分事业环境因素和组织过程资产? 事业环境因素:事业环境因素指围绕项目或能影 ...

  2. Android SDK Manager 中如果没有相应的镜像ARM XX Image

    Android SDK Manager 中如果没有相应的镜像ARM XX Image 处理做法是:先更新 相应版本Android SDK Tools 然后出现 ARM XX Image

  3. Android之Adapter用法总结-(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

  4. 剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计算法找其中的一个数

    题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 2 4 9 12 4 7 10 13 6 8 11 ...

  5. 标准MDL方法修改Page、NonPage内存的属性

    typedef struct _REPROTECT_CONTEXT { PMDL   Mdl; PUCHAR LockedVa; } REPROTECT_CONTEXT, * PREPROTECT_C ...

  6. 【MongoDB】2.可视化工具的安装和使用

    首先:关于  能支持MongoDB新版本的可视化工具,争议不断,各人都有各人的支持. 因此之前选择安装的时候就安装了MongoDB  3.0.14版本的. 最终,确定使用Robomongo作为我第一个 ...

  7. 从零开始---控制台用c写俄罗斯方块游戏(1)

    从零开始---控制台用c写俄罗斯方块游戏(1) 很少写博文,一来自身知识有限,二来自己知道,已经有很多这样的博文了,三就是因为懒,文笔也一般,四来刚出来工作,时间也不多 之所以写这篇博文,是因为应群里 ...

  8. BestCoder Round #83

    第一次做BC呀,本来以为会报零的,做了56分钟A了第一题 然后就没有然后了. 贴一下第一次A的代码. /* 0.组合数 1. 2016-05-14 19:56:49 */ #include <i ...

  9. HTTP基础08--追加协议

    消除 HTTP 瓶颈的 SPDY HTTP 的瓶颈 Web 网站为了保存这些新增内容,在很短的时间内就会发生大量的内容更新;为了尽可能实时地显示这些更新的内容,服务器上一有内容更新,就需要直接把那些内 ...

  10. [工作bug]c:import参数传递问题解析

    一.起因: 在项目的工厂中,由于某个界面根据产品种类显示的产品属性均不相同,所以决定将界面进行拆分,将每一个产品写入一个jsp界面,分别命名为product0.jsp.product1.jsp,在主界 ...