前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode: 65. Valid Number [✗]

leetcode: 65. Valid Number [✗]

作者头像
JNingWei
发布2018-09-27 17:05:59
5340
发布2018-09-27 17:05:59
举报
文章被收录于专栏:JNing的专栏JNing的专栏

problem

代码语言:javascript
复制
# 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.
# You should gather all requirements up front before implementing one.

Idea

代码语言:javascript
复制
这是一道和算法无关的题,压根不值得花时间做。

AC

代码语言:javascript
复制
class Solution():
    def isNumber(self, s):
        s = s.strip()
        if " " in s:
            return False
        if s.startswith('0') or s.lower().startswith('0b'):
            try:
                int(s, 2)
            except:
                pass
            else:
                return True
        if s.startswith('0'):
            try:
               int(s, 8)
            except:
                pass
            else:
                return True 
        if s.lower().startswith('0x'):
            try:
               int(s, 16)
            except:
                pass
            else:
                return True
        try:
           int(s, 10)
        except:
            pass
        else:
            return True
        try:
            float(s)
        except:
            pass
        else:
            return True

        return False


if __name__ == "__main__":
    assert Solution().isNumber(" 0.1 ") == True
    assert Solution().isNumber("abc") == False
    assert Solution().isNumber("1 a") == False
    assert Solution().isNumber("2e10") == True
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年11月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • problem
    • Idea
    • AC
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档