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. 网络-数据包在路由转发过程中MAC地址和IP地址,变与不变

    关于MAC地址和IP地址在传输过程中变与不变的问题: 结论:MAC地址在同一个广播域传输过程中是不变的,在跨越广播域的时候会发生改变的:而IP地址在传输过程中是不会改变的(除NAT的时候),总结为 路 ...

  2. C# 中DataTable转成模型List

    C# 中DataTable转成模型List 引入using System.Reflection; 命名空间 使用注意实体类的属性名必须和DataTable的列名一致 使用: DBList<Sto ...

  3. Visual Studio 调试技巧

    .net程序开发工具我都用vs(visual studio),开发过程中的跟踪调试最常用的就是断点跟踪调试了,但是现在才发现,用了这么多年vs断点跟踪调试是白用了啊.它居然还可以有这么多用法. 设置断 ...

  4. js键盘事件兼容浏览器

    document.onkeydown=function(event){ var e = event || window.event || arguments.callee.caller.argumen ...

  5. html之块级标签h系列,div

    两个名词含义: 块级标签:内容再少也会占满整行 内联标签:有多少内容点多少地方 注:块级标签和内联不签不是绝对的,可以通过CSS作任意转换 1.h1-h6 :块级标签 请仅仅把标题标签用作标题文本,如 ...

  6. python之os模块

    #!/usr/bin/env python3# _*_ coding:utf-8 _*_ import os os.getcwd()#os.chdir('path')print(os.curdir)p ...

  7. bootstrap3-typeahead 自动补全

    很酷的一个自动补全插件 http://twitter.github.io/typeahead.js 在bootstrap中使用typeahead插件,完成自动补全 相关的文档:https://gith ...

  8. MySQLAdmin用法

    MySQLAdmin用法用于执行管理性操作.语法是:shell> mysqladmin [OPTIONS] command [command-option] command ...通过执行mys ...

  9. DataGridView 添加行号

    private void dataGridViewX1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //Dat ...

  10. 程序员书单_sshi框架篇

    Struts2权威指南完整版 http://download.csdn.net/detail/shenzhq1980/9103955 精通struts.基于MVC的.java.web设计与开发http ...