版权声明: 本文为博主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. 运行报错:java.io.IOException: invalid constant type: 15

    jdk,tomcat更新到jdk1.8与 tomcat8 运行报错:java.io.IOException: invalid constant type: 15 pom.xml文件中更新javassi ...

  2. Android-Animation (1) 视图动画

    Android-Animation (1) 视图动画 学习自: Keegan小钢 :http://keeganlee.me/post/android/20151003 Carson_Ho : http ...

  3. 编译ROCKSDB总结

    Rocksdb是挺好的一个东西,就是取得一个可用的库太麻烦.之前我是用的rocksdbsharp里面他有编译好windows 和 linux的库 兼 容性还挺好,ubuntu win10 直接跑没毛病 ...

  4. 同步计数器 CountDownLatch

    CountDownLatch 是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行. CountDownLatch 是通过一个计数器来实现的,计数器的初始值为线程的数量.每 ...

  5. H5上传功能

    近期开发一个关于微信公总号二次开发中,上传图片的需求,测试几个开源插件,更新一些心得,有需要可留言!!! plupload plupload多张上传图片的一个参考demo ajaxFileUpload ...

  6. MicroSoft CryptoAPI data/file encrypt/decrypt

    linux 用第三方库 Crypto++, 还未实战. CryptoAPI使用两种密钥:会话密钥与公共/私人密钥对.会话密钥使用相同的加密和解密密钥,这种算法较快,但必须保证密钥的安全传递.公共/私人 ...

  7. ProgressBar三种style

    一.普通的ProgressBar显示如图 <ProgressBar        android:id="@+id/pbNormal"        android:layo ...

  8. Edge Intelligence: On-Demand Deep Learning Model Co-Inference with Device-Edge Synergy

    边缘智能:按需深度学习模型和设备边缘协同的共同推理 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文. ...

  9. QEMU KVM Libvirt手册(9): network

    虚拟网卡由-net nic定义 # qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntutest.img -vnc ...

  10. 微软75亿收购Github,微软以开发者为中心的初心不变

    前天关于微软要收购 GitHub 的消息传出后,很多人都纷纷讨论,希望 GitHub 能够独立存在,不被任何大厂收购,可是 GitHub 也要生存啊.那又有人说:希望是 Google 来收购 GitH ...