题目来源:

  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. C++面试经常涉及的概念1

    1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应.free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,ne ...

  2. MVC模式和URL访问

    一.什么是MVC //了解 M -Model 编写model类 对数据进行操作 使用Model类 来操作数据 V -View 编写html文件,页面呈现 C -Controller 编写类文件(Use ...

  3. cocos2dx tag和zorder

    当一个渲染对象加入到两外一个渲染对象中时,可以有两个可选参数,一个时tag,一个是order virtual void addChild(CCNode * child); virtual void a ...

  4. STL内存配置器

    一.STL内存配置器的总体设计结构 1.两级内存配置器:SGI-STL中设计了两级的内存配置器,主要用于不同大小的内存分配需求,当需要分配的内存大小大于128bytes时, 使用第一级配置器,否则使用 ...

  5. jquery简单判断PC端还是移动端

    $(function(){ if (!navigator.userAgent.match(/mobile/i)) { //PC端 }else{ //移动端 } })

  6. BaseAdapter使listview设置不同背景图片并添加selector

    前段时间为了实现根据item不同的内容实现不同的背景色google了好久只找到了个隔行换色,通过自定义SimpleAdapter终于实现了此功能,但是定义了selector并没有触发点击效果.今天重新 ...

  7. Android仿人人客户端(v5.7.1)——个人主页(三)

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/9405089 声明:仿人人项目,所用所有图片资源都来源于其它Android移动应用,编 ...

  8. 用JS判断两个数字的大小

    js中的var定义的变量默认是字符串,如果单纯的比较字符串的话,会出现错误,需要先转化为int类型在做比较. [备注:110和18在你写的程序中是18大的,因为 这两个数都是字符串,而1和1相等之后比 ...

  9. js 计算两个时间差

    /* * 计算两个日期的间隔天数* BeginDate:起始日期的文本框,格式為:2012-01-01* EndDate:結束日期的文本框,格式為:2012-01-02* 返回兩個日期所差的天數* 調 ...

  10. Cookie管理

    1,判断来访者是否第一次 public class VistorTest extends HttpServlet { @Override protected void doGet(HttpServle ...