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. android 消息推送

    android 消息推送 极光推送百度云推送(语音)友盟消息推送

  2. eclipse文本编码格式修改为UTF-8 (转)

    如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...

  3. matlab练习程序(碎片)

    这个算法是对photoshop中滤镜->像素化->碎片这个功能的学习. PS这个功能好像不带参数,不过我这里有滤波半径r可以进行控制. 因为我是看效果猜算法的,效果肯定有所区别. 我的想法 ...

  4. 在Salesforce中向Page Layout中添加Visualforce Page

    在Salesforce中可以向Object所对应的Layout中添加我们自定义的Visualforce Page. 此时的Visualforce Page与Asp.Net MVC中的Partial V ...

  5. Css3 提示框

    <div class="arrow_box"><span>xxy is a so cool boy</span></div> css ...

  6. hdu1087 最大递增子段和

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1087 状态方程:sum[j]=max{sum[i]}+a[j]; 其中,0<=i<=j, ...

  7. SQL 最基本使用

    --创建表 CREATE TABLE TreeData (id INT IDENTITY(1,1) PRIMARY KEY , pid INT ) --为表添加列 ALTER TABLE treeda ...

  8. jquery.color.js的使用

    Jquery本身不支持变色,Jquery Color.js弥补了这缺陷并为animate动画赋予变色效果,如下红变蓝后执行回调再由蓝变红. <!DOCTYPE html> <html ...

  9. Codeforces Round #342 (Div. 2)

    贪心 A - Guest From the Past 先买塑料和先买玻璃两者取最大值 #include <bits/stdc++.h> typedef long long ll; int ...

  10. iOS学习35数据处理之文件读写

    1. 沙盒机制 1> 沙盒概述 每一个iOS应用程序都会为自己创建一个文件系统目录(文件夹), 这个独立.封闭.安全的空间,叫做沙盒 注:① 每一个应用程序都会拥有一个应用程序沙盒 ② 应用程序 ...