加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

leetcode——65. 有效数字

发布时间:2020-12-20 10:20:36 所属栏目:Python 来源:网络整理
导读:我太难了,俩小时。。。。。。 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 : i

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读