版权声明: 本文为博主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. jmeter主要函数助手功用说明

    jmeter中虽然有很多的插件,但是有些需要安装,有些具有一定的局限性.函数助手是一个快捷的工具库.下面记录一下函数助手中一些主要的函数的使用方法. 注:不内容中所有的实例均基于3.2记录 1._Be ...

  2. Springmvc 横向源码原理解析(原创)

    1.springmvc的基本流程(不多赘述) 2.主要涉及到的类 //该方法返回HandlerExecutionChain 类 并不是直接返回handler 是因为在HandlerExecutionC ...

  3. maven项目如何手动打包

    1.确定打包对象:dubbo-admin 2.进入打包对象目录,复制目录路径(D:\H\dubbox-master\dubbo-admin). 可以看到该对象为典型的maven目录,此时没有“targ ...

  4. Windows10用fiddler抓包Android应用(解决手机设置代理后无法上网,设置只抓app包)

    1.环境准备 1.电脑上安装fiddler 2.手机和电脑在同一个局域网内 2.设置 1.fiddler>Tools>Fiddler Options>Connections 勾选Al ...

  5. H5_ 多媒体video,autio使用示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Shell 脚本处理用户输入

    传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户的输入 bash shell提供了一些不同的方法来从用户处获取数据,包括命令行参数(添加在命令后数据),命令行选项(可以修改命令行为的单个 ...

  7. springboot整合mybatis和mybatis-plus

    问题 1 分页查询问题 2   mybatis的配置由mybatis变成mybatis-plus 3  Mybatis-plus中的Wrapper

  8. Linux 搭建 Nginx+PHP-FPM环境

    安装PHP.Nginx和PHP-FPM sudo apt-get install php sudo apt-get install nginx sudo apt-get install php7-fp ...

  9. H5混合开发问题总结

    1.This application is modifying the autolayout engine from a background thread, which can lead to en ...

  10. npm修改淘宝原

    //修改之前查看一下npm config get registry https://registry.npmjs.org/ //设置源npm config set registry https://r ...