题目来源:

  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. 在XAML代码中为节点树安装事件监听器

    通过以下的演示样例代码,能够发现,我们能为随意的节点指定要监听的路由事件,而这个路由事件本身和这个元素可能根本就没有关系. <Window x:Class="Demo002.MainW ...

  2. java final 关键字醍醐灌顶

    醍醐灌顶: final 关键字,它可以修饰数据 .方法.类. 可能有些同学傻傻分不清出,这里可以快速弄懂final; final 实例域: 可以将实例域定义为final,构建对象时必须初始化这样的域, ...

  3. UVA1471( LIS变形)

    这是LIS的变形,题意是求一个序列中去掉某个连续的序列后,能得到的最长连续递增序列的长度. 用DP的解法是:吧这个序列用数组a来记录,再分别用两个数组f记录以i结尾的最长连续递增序列的长度,g[i]记 ...

  4. Median of Sorted Arrays

    (http://leetcode.com/2011/03/median-of-two-sorted-arrays.html) There are two sorted arrays A and B o ...

  5. 加密PHP文件的方式,目测这样可以写个DLL来加密了

    <?php function encode_file_contents($filename) { $type=strtolower(substr(strrchr($filename,'.'),1 ...

  6. phplib系统开发经验总结

    数据库设计: 数据库的设计一定要在了解整个系统需求的情况下,把数据库设计,及ER图画出来,数据库字典也要及时把握,只有掌握了这些才能下手开始设计界面,后期如果有需要,可以在数据库中添加数据,但要及时更 ...

  7. python初探-copy

    python中,数据的拷贝有以下三种形式:赋值.浅copy和深copy.根据类型的不同,可以把数据分成以下两类:字符串和数字为一类,其他(包括列表.元祖.字典...)为一类. 在python中有池的概 ...

  8. 国内外主流BI厂商对比

    BI(Business Intelligence),即商业智能或者商务智能,它是一套完整的解决方案,用来将企业中现有的数据进行有效的整合,快速准确的提供报表并提出决策依据,帮助企业做出明智的业务经营决 ...

  9. 以程序的方式操纵NTFS的文件权限(陈皓)

    http://blog.csdn.net/haoel/article/details/2905 http://blog.sina.com.cn/s/blog_7f91494101018nmn.html

  10. Js动态设置Img大小

    function ResizePic() {        $('img').each(function () {            var maxWidth = 450; // 图片最大宽度   ...