题目来源:

  https://leetcode.com/problems/valid-number/


题意分析:

  输入一个字符串,判断这个字符串表示的是不是一个有效的数字。比如:

  "0" => true
  " 0.1 " => true
  "abc" => false
  "1 a" => false
  "2e10" => true


题目思路:

  由于空格在两端不影响,所以首先将两端的空格去掉。判断开始是否数字,如果不是直接返回False,然后判断是否遇到'.'和‘e',然后判断'e'以后是否有’+‘,’-‘和’.'。


代码(Python):

  

 class Solution(object):
def isNumber(self, s):
"""
:type s: str
:rtype: bool
"""
begin,last =0,len(s) - 1
while begin <= last and s[begin] == ' ': begin += 1
while begin <= last and s[last] == ' ': last -= 1
if begin < last and (s[begin] == '+' or s[begin] == '-'): begin += 1
num,dot,exp = False,False,False
while begin <= last:
if s[begin] >= '' and s[begin] <= '':
num = True
elif s[begin] == '.':
if dot or exp:
return False
dot = True
elif s[begin] == 'e' or s[begin] =='E':
if exp or not num:
return False
exp,num = True,False
elif s[begin] =='+' or s[begin] == '-':
if begin == 0 or s[begin - 1] != 'e':
return False
else:
return False
begin += 1
return num

转载请注明出处:http://www.cnblogs.com/chruny/p/5028726.html

[LeetCode]题解(python):065-Valid Number的更多相关文章

  1. Java for LeetCode 065 Valid Number

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

  2. LeetCode之“字符串”:Valid Number(由此引发的对正则表达式的学习)

    题目链接 题目要求: Validate if a given string is numeric. Some examples: "0" => true " 0.1 ...

  3. Leetcode 详解(Valid Number)

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

  4. LeetCode(65) Valid Number

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

  5. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  6. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. 【LeetCode】65. Valid Number

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

  8. [LeetCode 题解]:Palindrome Number

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine ...

  9. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  10. 【leetcode】Valid Number

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

随机推荐

  1. 如何解决dns解析故障

    在实际应用过程中可能会遇到DNS解析错误的问题,就是说当我们访问一个域名时无法完成将其解析到IP地址的工作,而直接输入网站IP却可以正常访问,这就是因为DNS解析出现故障造成的.这个现象发生的机率比较 ...

  2. 有关Gcd,Lcm的一点小结论

    先介绍两个: 大数的Gcd Stein+欧几里德 function stein(a,b:int64):int64; begin if a<b then exit(stein(b,a)); the ...

  3. gdbserver 安卓apk

    gdbserver  调试程序 底层调用c/c++ 动态库, 动态库带调试选项 查看手机IP 192.168.1.177 包所调用的c/c++ 库是在/data/data/包名/lib/ 下 1.将安 ...

  4. Android 架构【转】

    import java.util.ArrayList; import java.util.List;   import android.app.Activity; import android.app ...

  5. .NET + Jcrop 实现在线裁图功能

    最近有这样一个需求,因为一个门户网站首页展示图片很长但很矮,自己截图怕有不到位,所以利用JQUERY 的 Jcrop组件做了一个在线裁图的功能. 初始化 $('#oldpicImg').Jcrop({ ...

  6. git 使用笔记(三)-分支的使用

    简单介绍 之前说过,每次修改之后,Git 并不是保存这些修改之后的差异变化,实际上就像一个照相机一样,将修改后的文件拍下作为文件快照,记录在一个微型的文件系统中.在 Git 中提交时,会保存一个提交对 ...

  7. w3c教程

    http://www.w3cfuns.com/course.php http://www.w3cfuns.com/home.php?mod=space&uid=5434413&do=b ...

  8. spring IOC简单入门

    spring的核心是ioc和aop 先介绍一下IOC(inverse of control控制反转)又叫DI(Dependency injection依赖注入) 个人理解为把对象的控制权由类转移到配置 ...

  9. iOS中的地图和定位

    文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location  如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...

  10. UVa1583 Digit Generator

    #include <stdio.h> int main(){    int T, N, i, k, digitsum, generator;    scanf("%d" ...