题目来源:

  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. cpan安装及其使用

    cpan安装及其使用 Perl是一种相当灵活的程序编程语言,现有的许有程序都是使用它进行编程的.它的优点之一就是无需自己编写编码,你就能利用许多增加的模块,创建新的功能. 程序利用这些模块的编码,而程 ...

  2. 脑波设备mindwave二次开发框架

    神念科技提供的mindwave提供了脑波耳机和相应的游戏,这些游戏你可以通过购买神念科技的mindwave耳机来获取,这里不多作介绍. 我们作为程序员,如果有了相应的创意,也可以通过他们提供的二次开发 ...

  3. Hibernate 配置详解(12) 其实我也不想用这么土的名字

    hibernate.hbm2ddl.import_files 这个配置用于在hibernate根据映射文件执行DDL之前,如果我们自己设置了要事先运行的SQL文件,hibernate就会先执行这些SQ ...

  4. 移动互联与大数据之美-逐浪CMS2 X1.1发布

    北京时间2013年7月1日: 领先的CMS研发软件厂商--上海逐浪CMS软件科技有限公司发布其年中重要更新,并以Zoomla!逐浪CMS2 X1.1为版本号向全球用户投递新版软件. 此次更新包括: 1 ...

  5. javascript绑定事件

    本质:不同的库或者工具中总是封装了不同的事件绑定形式,但是究其根源,还是IE事件模型和W3C事件模型不同的处理方式 1)W3C事件模型:支持事件捕捉和冒泡 addEventListener('type ...

  6. ZOJ3689 Digging(01背包)

    #include <iostream> #include <cstdio> #include<cmath> #include<algorithm> #i ...

  7. Android应用开发基础篇(5)-----Handler与多线程

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/19/2358155.html 一.概述 Handler这个类主要用来发送和处理消息的.它有多个发 ...

  8. BZOJ 2724: [Violet 6]蒲公英( 分块 )

    虽然AC了但是时间惨不忍睹...不科学....怎么会那么慢呢... 无修改的区间众数..分块, 预处理出Mode[i][j]表示第i块到第j块的众数, sum[i][j]表示前i块j出现次数(前缀和, ...

  9. Hadoop 相关问题

    1.MR Job 输入非常多,启动map 非常多,如何提高MapTask 启动速度(附加条件:集群很空闲,资源多多): 参考答案: a.重写调度器算法,降低时间复杂度 b.Out-of-bound h ...

  10. Linux 网络编程: echo Service

    前言 大病初愈,感谢某人的陪伴,感谢王乐庆同学和赵攀同学的细心照顾.原以为过了第八周就不忙了,却没想到还有明天的党章考试.还是写代码比背党章有意思~趁着服务器还没过期,赶紧把 echo 完成了.关于错 ...