zoukankan      html  css  js  c++  java
  • leetcode——65. 有效数字

    我太难了,俩小时。。。。。。

    class Solution(object):
        def isNumber(self, s):
            """
            :type s: str
            :rtype: bool
            """
            s=s.strip()
            for i in range(len(s)):
                if s[i] not in 'e0123456789+-.':
                    return False
            if len(s)<1:
                return False
            if len(s)==1:
                if s[0] not in '0123456789':
                    return False
            if s[0] in 'e' or s[-1] in 'e+-':
                return False
            if s.count('e')>1:
                return False
            if s.count('e')==1:
                if s.count('.')>1:
                    return False
                elif s.count('.')==1:
                    #print('gygufyufu')
                    if s[0]=='.' and s[1]=='e':
                        return False
                    if s.index('e')==s.index('.')-1:
                        return False
                    if s.index('.')>s.index('e'):
                        return False
                    if s.index('.')<s.index('e'):
                        if s[s.index('.')+1] in '+-':
                            return False        
                    if s[-1]!='.':
                        return True
                    else:
                        return False
                    if s.count('+')+s.count('-')>2:
                        return False
                    elif s.count('+')+s.count('-')==2:
                        if s[0] in '+-' and s[s.index('e')+1] in '+-':
                            return True
                        else:
                            return False
                    
                    elif s.count('+')+s.count('-')==1:
                        if s[0] in '+-' or s[s.index('e')+1] in '+-':
                            return True    
                        else:
                            return False
                else:
                    if s.count('+')+s.count('-')>2:
                        return False
                    elif s.count('+')+s.count('-')==2:
                        if s[0] in '+-' and s[s.index('e')+1] in '+-':
                            return True
                        else:
                            return False
                    elif s.count('+')+s.count('-')==1:
                        if s[0] in '+-' :
                            if s[1]=='e':
                                return False
                            else:
                                return True    
                        elif s[s.index('e')+1] in '+-' and len(s)>s.index('e')+2:
                            return True
                        else:
                            return False
            else:
                if s.count('.')>1:
                    return False
                elif s.count('.')==1:
                    if s.count('+')+s.count('-')>1:
                        return False
                    elif s.count('+')+s.count('-')==1:
                        if s[0] in '+-' :
                            if len(s)==2:
                                if s[1]=='.':
                                    return False
                                else:
                                    return True
                            elif len(s)>2:
                                if s[1]=='.' and s[2] not in '0123456789':
                                    return False
                                else:
                                    return True
                            else:
                                return True    
                        else:
                            return False
                    if s[s.index('.')-1] in '+-':
                        return False
                else:
                    if s.count('+')+s.count('-')>1:
                        return False
                    elif s.count('+')+s.count('-')==1:
                        if s[0] in '+-':
                            return True    
                        else:
                            return False
            return True
    执行用时 :28 ms, 在所有 Python 提交中击败了70.92%的用户
    内存消耗 :11.7 MB, 在所有 Python 提交中击败了24.00%的用户
     
    执行用时为 12 ms 的范例
    class Solution(object):
        def isNumber(self, s):
            """
            :type s: str
            :rtype: bool
            """
            if not s:
                return False
                
            s = s.strip()
            sci = s.split('e')
            if len(sci) > 2:
                return False
            if len(sci) == 1:
                return False if not sci[0] else self.is_num(s)
                
            signs = ['-', '+']
            sci[1] = sci[1] if not sci[1] or sci[1][0] not in signs else sci[1][1:]
            
            return self.is_num(sci[0]) and self.is_int(sci[1])
            
        def is_num(self, s):
            if not s:
                return False
                
            signs = ['-', '+']
                
            parts = s.split('.')
            if len(parts) > 2:
                return False
            
            parts[0] = parts[0] if not parts[0] or parts[0][0] not in signs else parts[0][1:]
            if len(parts) == 1:
                return self.is_int(parts[0])
                
            if not parts[0] and self.is_int(parts[1]):
                return True
            
            if not parts[1] and self.is_int(parts[0]):
                return True
                
            return self.is_int(parts[0]) and self.is_int(parts[1])
            
        def is_int(self, s):
            if not s:
                return False 
                
            for i in range(0, len(s)):
                if not s[i].isdigit():
                    return False
                    
            return True

    没仔细看。。。

                                                                                           ——2019.10.11

     
     

    自己还是不会做,看了题解,学到了,简单清晰
    public boolean isNumber(String s) {//可带有的字母是e,但是不能位于首位和末尾
            if(s.length() == 0){
                return false;
            }
            boolean num_seen = false;
            boolean has_e = false;  //是否有e出现,是否合法
            boolean has_dot = false;   //是否有小数点出现,是否合法
            s = s.trim();
            for(int i = 0;i<s.length();i++){
                if(s.charAt(i) <= '9' && s.charAt(i) >= '0'){
                    num_seen = true;
                }else if(s.charAt(i) == '.'){
                    if(has_dot || has_e){
                        return false;
                    }
                    has_dot = true;
                }else if(s.charAt(i) == 'e' || s.charAt(i) == 'E'){
                    if(has_e || !num_seen){
                        return false;
                    }
                    has_e = true;
                    num_seen = false;
                }else if(s.charAt(i) == '+' || s.charAt(i) == '-'){
                    if(i != 0 && s.charAt(i-1) != 'e' && s.charAt(i-1) != 'E'){
                        return false;
                    }
                }else{
                    return false;
                }
            }
            return num_seen;
        }

    * 1.判断是否属于数字的0-9区间
    * 2.遇到点的时候,判断前面是否有点或者E,都需要return false
    * 3.遇到E的时候,判断前面数字是否合理,是否有E,并把num_seen置为false,防止E后无数字
    * 4.遇到+-的时候,判断是否是第一个,如果不是,判断是否在E后面,都不满足return false
    * 5.其他情况都是false

     ——2020.7.7

     
     
     
     
     
     
     
     
     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    二、一切都是对象
    一、对象导论
    CSS 属性大全
    CSS颜色代码大全
    CSS Position 定位属性
    CSS Box Model 盒子模型
    ThreadLocal
    Java 模拟死锁
    byte 最小值为啥是最小是 -128 ;int最小值为啥是 -2147483648
    cmd 查看端口号占用情况
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11656562.html
Copyright © 2011-2022 走看看