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.

直接上代码,特别是解法二,引入 re 模块匹配,很精简强大!

 class InputType:
INVALID = 0
SPACE = 1
SIGN =2
DIGIT =3
DOT = 4
EXPONENT =5 class Solution:
# @param s, a string
# @return a boolean
def isNumber(self, s):
transition_table = [[-1, 0, 3, 1, 2, -1], # next states for state 0
[-1, 8, -1, 1, 4, 5], # next states for state 1
[-1, -1, -1, 4, -1, -1], # next states for state 2
[-1, -1, -1, 1, 2, -1], # next states for state 3
[-1, 8, -1, 4, -1, 5], # next states for state 4
[-1, -1, 6, 7, -1, -1], # next states for state 5
[-1, -1, -1, 7, -1, -1], # next states for state 6
[-1, 8, -1, 7, -1, -1], # next states for state 7
[-1, 8, -1, -1, -1, -1]] # next states for state 8 state = 0
for char in s:
inputType = InputType.INVALID
if char.isspace():
inputType = InputType.SPACE;
elif char == '+' or char == '-':
inputType = InputType.SIGN
elif char in '':
inputType =InputType.DIGIT
elif char == '.':
inputType = InputType.DOT
elif char == 'e' or char =='E':
inputType = InputType.EXPONENT state = transition_table[state][inputType]
if state == -1:
return False
return state==1 or state==4 or state ==7 or state==8 def isNumber2(self,s):
import re
return bool(re.match("^\s*[\+\-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?\d+)?\s*$",s))

Valid Number @python的更多相关文章

  1. [leetcode]Valid Number @ Python

    原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...

  2. 【leetcode】Valid Number

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

  3. [LintCode] Valid Number 验证数字

    Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...

  4. 配置域名服务器报错named[822]: dns_rdata_fromtext /etc/bind/db.asertest.com mail not a valid number

    问题描述: 为了配置邮件服务器,更改了相关域名,改完后,重启bind9报错 Mar 17 14:39:39 DnsServer2 named[822]: dns_rdata_fromtext: /et ...

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

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

  6. [leetcode]65. Valid Number 有效数值

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

  7. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  8. LeetCode: Valid Number 解题报告

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

  9. leetCode 65.Valid Number (有效数字)

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

随机推荐

  1. 使用substring和split方法从字符串中抽取一组清单

    提取前: '这是一个句子.这是一个句子,包含了一列清单: 樱桃, 橙子, 桔子, 苹果, 香蕉.这是清单内容.'; 提取后: ["樱桃", " 橙子", &qu ...

  2. 在easyui datagrid中formatter数据后使用linkbutton

    http://ntzrj513.blog.163.com/blog/static/2794561220139245411997/ formatter:function(value,rowData,ro ...

  3. 开放平台-web实现人人网第三方登录

    应用场景     web应用通过人人网登录授权实现第三方登录.   操作步骤     1  注册成为人人网开放平台开发者         http://app.renren.com/developer ...

  4. 用Javascript弹出div定义的消息框并往块里面填写文字

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. javascript中href和replace比较

      在使用javascript的时候,有时候对于经常使用的方法太熟悉而忽略了他们之间原理的细微差别.举例如下:window.location.href,window.location.replace. ...

  6. Microsoft Dynamics CRM2011 必备知识点

    一.CRM基本知识 1.CRM2001 有几个服务端点? 答:对外公开的服务,如Web服务,WCF,Restful API 2.一个ERP系统,要访问CRM的数据,CRM2011有哪些现有的服务入口提 ...

  7. 将war文件解压到指定目录

    问:如何将.war文件解压到指定目录? 答:jar命令没有这样的选项. eg:将abc.war解压到当前文件夹? 答:进入目标文件即abc.war文件所在的文件夹,按住shift键并在该文件夹空白处点 ...

  8. 使用匿名委托,Lambda简化多线程代码

    使用匿名委托,Lambda简化多线程代码   .net中的线程也接触不少了.在多线程中最常见的应用莫过于有一个耗时的操作需要放到线程中去操作,而在这个线程中我们需要更新UI,这个时候就要创建一个委托了 ...

  9. EnterpriseLibrary之Caching应用

    http://blog.csdn.net/linux7985/article/details/6239433 http://cache.baiducontent.com/c?m=9f65cb4a8c8 ...

  10. OC中的self指针

    在OC中,self是一个指针,方法和属性可以通过self.function进行访问:成员变量使用self->变量名进行访问.调用方法还可以用[self function]; OC中的self有点 ...