[LeetCode]题解(python):065-Valid Number

题目来源:

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



题意分析:

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

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



题目思路:

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



代码(Python):

  

 1 class Solution(object):
 2     def isNumber(self, s):
 3         """
 4         :type s: str
 5         :rtype: bool
 6         """
 7         begin,last =0,len(s) - 1
 8         while begin <= last and s[begin] == ‘ ‘: begin += 1
 9         while begin <= last and s[last] == ‘ ‘: last -= 1
10         if begin < last and (s[begin] == ‘+‘ or s[begin] == ‘-‘): begin += 1
11         num,dot,exp = False,False,False
12         while begin <= last:
13             if s[begin] >= ‘0‘ and s[begin] <= ‘9‘:
14                 num = True
15             elif s[begin] == ‘.‘:
16                 if dot or exp:
17                     return False
18                 dot = True
19             elif s[begin] == ‘e‘ or s[begin] ==‘E‘:
20                 if exp or not num:
21                     return False
22                 exp,num = True,False
23             elif s[begin] ==‘+‘ or s[begin] == ‘-‘:
24                 if begin == 0 or s[begin - 1] != ‘e‘:
25                     return False
26             else:
27                 return False
28             begin += 1
29         return num
30         



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

时间: 2024-10-15 15:31:54

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

Java for LeetCode 065 Valid Number

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.

Leetcode题解(8):L179/Largest Number

L179:Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a st

(leetcode题解)Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). Example 1: Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1. Examp

Leetcode 详解(Valid Number)

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 解决方案: public class Solution { public boolean isNumber(String s) { in

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode][JavaScript]Valid Number

https://leetcode.com/problems/valid-number/ Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

Valid Number leetcode java

题目: 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 ambigu

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略:

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible