版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址

http://blog.csdn.net/lzuacm

Leetcode 65. 有效数字

Leetcode 65. Valid Number

在线提交:

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

类似问题 - PAT 1014_牛客网

https://www.nowcoder.com/pat/6/problem/4050


题目描述

验证给定的字符串是否为数字(科学计数法)。

例如:

“0” => true

” 0.1 ” => true

“abc” => false

“1 a” => false

“2e10” => true

说明: 我们有意将问题陈述地比较模糊。在实现代码之前,你应当事先思考所有可能的情况。

更新于 2015-02-10:

C++函数的形式已经更新了。如果你仍然看见你的函数接收 const char *类型的参数,请点击重载按钮重置你的代码。


  ●  题目难度: Hard

思路:

按照题意,满足要求的数形如: ☐ ±4.36e±05☐ ,其中☐表示首尾的若干个连续的空格。

可更具体地表示为:☐ ±double e±0…0int+☐ (当然此处的int是long long的, 或int64的,而0…0是若干个连续的0)。而对于特例”0e”,该串中e后为空串,应返回false。事实上 ±double可以直接看作double,±0…0int可直接看作int。

需测试的Test Case:

"0e-1"
"0"
" 0.1 "
"abc"
"1 a"
" 2e10 "
"+ 1"
"5e001"
"44e016912630333"
"2e0"
"2e00"
"0e"
" +4.36e-01"

Expected answer:

true
true
true
false
false
true
false
true
true
true
true
false
true

已AC代码:

public class Solution
{
    public bool IsNumber(string s)
    {
        s = s.Trim();
        string[] arr = s.Split('e');
        // var hasSign = arr[0].IndexOf("+", StringComparison.Ordinal) == 0 || arr[0].IndexOf("-", StringComparison.Ordinal) == 0;
        // string newPart1 = hasSign ? arr[0].Substring(1) : arr[0];
        string newPart1 = arr[0];
        if (newPart1.IndexOf(" ", StringComparison.Ordinal) >= 0)
            return false;
        bool isPart1Double = double.TryParse(newPart1, out var part1);
        string newPart2 = arr.ElementAtOrDefault(1);
        if (newPart2 == String.Empty) // handle test case like: "0e"
            return false;

        if (newPart2 != null)
        {
            foreach (char ch in newPart2)
            {
                if (ch == '0')
                    newPart2 = newPart2.Substring(1);
            }
        }

        bool isPart2Int = Int64.TryParse(newPart2, out var part2);
        if (arr.Length == 1)
        {
            if (isPart1Double)
                return true;
        }

        if (arr.Length == 2)
        {
            if (isPart1Double && newPart2 == String.Empty)
                return true;
            if (isPart1Double && isPart2Int)
                return true;
        }

        return false;
    }
}

Rank:

You are here! Your runtime beats 69.44% of csharp submissions.

1481 / 1481 test cases passed.

Runtime: 96 ms

C#版 - Leetcode 65. 有效数字 - 题解的更多相关文章

  1. Java实现 LeetCode 65 有效数字

    65. 有效数字 验证给定的字符串是否可以解释为十进制数字. 例如: "0" => true " 0.1 " => true "abc&q ...

  2. [LeetCode] 65. 有效数字

    题目链接 : https://leetcode-cn.com/problems/valid-number/ 题目描述: 验证给定的字符串是否可以解释为十进制数字. 例如: "0"` ...

  3. C#版 - Leetcode 13. 罗马数字转整数 - 题解

    C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...

  4. C#版 - Leetcode 504. 七进制数 - 题解

    C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...

  5. C#版 - Leetcode 633. 平方数之和 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  6. C#版 - Leetcode 593. 有效的正方形 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  8. C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解

    C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解 在线提交: https://leetcode.com/problems/bitwise-and-of-num ...

  9. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. centos7.x 安装 fastDFS

    环境准备 使用的系统软件 名称 说明 centos 7.x libfatscommon FastDFS分离出的一些公用函数包 FastDFS FastDFS本体 fastdfs-nginx-modul ...

  2. HBase数据库增删改查常用命令操作

    最近测试用到了Hbase数据库,新建一个学生表,对表进行增删改查操作,把常用命令贴出来分享给大家~ 官方API:https://hbase.apache.org/book.html#quickstar ...

  3. 元素定位-XPATH定位方法总结

    1.Xpath定位方法探讨 xpath是比较常用的一种定位元素的方式,因为它很方便,缺点是,消耗系统性能.如果Xpath使用的比较好,几乎可以定位到任何页面元素,而且受页面变化影响较小. 1.1.什么 ...

  4. linux服务器用ssh 公钥下载代码 创建公钥,添加

    ssh-keygen -t rsa -C "8....@qq.com"cd ~/.sshcat id_rsa.pub 粘贴到对应的 https://github.com/setti ...

  5. tensorflow 使用 5 mnist 数据集, softmax 函数

    用于分类  softmax 函数 手写数据识别:

  6. Spring源码Gradle

    Microsoft Windows [版本 10.0.17134.590](c) 2018 Microsoft Corporation.保留所有权利. D:\Workspaces\idea\sprin ...

  7. java编程目录

    目录结构 java常用数据类型使用 https://www.cnblogs.com/cerofang/p/10250535.html java中的排序  https://www.cnblogs.com ...

  8. Java for Andriod 第二周学习总结

    第四章 学习时遇到的问题或新知识点: 1. 构造方法.每个类至少有一个构造方法,且构造方法必须的名称必须与类名相同. 2. Varargs.允许方法拥有一个可变长度的参数列表. 3. 对象的内存分配. ...

  9. ubuntu系统下手动安装autoconf安装包

    首先简单介绍一下autoconf.Autoconf是一个可以适应多种unix类系统的shell脚本的工具. 我在往虚拟机中安装应用时,需要用到该工具,于是想下载一个.但是由于系统内核版本低,已不能用a ...

  10. java小练习

    打印99乘法表 因为有9行9列,所有要用两个for循环 int m; for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) ...