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. Unity3D研究院编辑器之脚本获取资源内存和硬盘大小

    内存 使用Profiler可以查看某个资源的内存占用情况,但是必须启动游戏,并且待查看的资源已经载入游戏中.我希望的是不启动游戏,也能看到它的内存好做统计. 硬盘 由于unity中的资源压缩格式记录在 ...

  2. php 数组排序 sort asort ksort

    <?php $arr = array('d'=>'sdf', 'r'=>'sdf', 'a'=> 'eee'); //sort($arr); // 对数组的值进行重排, 删除之 ...

  3. USACO/gift1

    描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人. 然而,在任何一群朋友中 ...

  4. MvcPager2.0 中分页初始化失败的问题

    页面初始化时只有一页数据,或没数据时,出现分页控件初始化失败以及后续Ajax分页功能失效的问题, <div class="pagin"> <div class=& ...

  5. python分割数组里面重复的元素

    c=[1,1,1,1,2,2,2,3,3,4,4,4,4,4,5,5,5,] a = [] x = [] for i in range(0,len(c)): if i + 1 < len(c): ...

  6. python之路之正则表达式

    匹配格式^ 匹配字符串的开头$ 匹配字符串的结尾. 除了换行符外的所有字符[...] 用来表示一组字符,,单独列出:[amk]匹配'a','m'或'k'[^..] 不在[]中的字符:[^abc]匹配除 ...

  7. 使用Axure制作App原型应该怎样设置尺寸?

    使用Axure制作的原型,如果你没有设置自适应视图的话它是不会自动适应任何设备的. 若要解释清楚这个问题需要的篇幅比较长,请大家自行参考 Point/Pixel/PPI/DPI 的意思和它们之间的关系 ...

  8. .NET 客户端上传本地excel文件到服务器上,并在客户端显示

    // 上传按钮 protected void btnUp_Click(object sender, EventArgs e) { bool b = Upload(fuExcel); // 上传exce ...

  9. Webproject 每次运行都停到workerDone(this); tomcat调试

    那是因为你是Debug调试,你要将Dubug的时间设置长一些; 设置步骤:     window-> preferences -> java -> debug -> commu ...

  10. SQL 查看数据库表的容量大小

    --==============查看数据库表的容量大小========start================================?============ Create Table # ...