65. Valid Number

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.

public bool IsNumber(string s) {
        int size = s.Length;
        if(size == 0) return false;
        int numindex = -1;
        int dotindex = -1;
        int eindex = -1;//e index
        var possibleFirstChar = new List<char>(){‘+‘,‘-‘,‘.‘};
         var digits = new List<char>(){‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘};
        if(size == 1 && !digits.Contains(s[0])) return false;
        //remove spaces at the beginning till not ‘ ‘
        int start = 0;
        while(start< size && s[start] == ‘ ‘)
        {
            start++;
        }
        if(start == size) return false;
        //first letter just
        if(!possibleFirstChar.Contains(s[start]) && !digits.Contains(s[start])) return false;
        if(possibleFirstChar.Contains(s[start]))
        {
            if(s[start] == ‘.‘) {
                dotindex = start;
            }
            start++;
        }

        int tempStart = start;
        int i =tempStart;
        for(;i<size && s[i] != ‘ ‘;i++)
        {
            if(s[i] == ‘e‘)
            {
                if(eindex >= 0 || numindex < 0) return false;// cannot have 2 ‘e‘ && must have digits before e
                eindex = i;
            }
            else if(s[i] == ‘.‘)
            {
                 if(eindex >= 0 || dotindex >= 0) return false; // cannot after ‘2‘ && can not have 2 ‘.‘
                 dotindex = i;
            }
            else if(s[i] == ‘+‘ || s[i] == ‘-‘) //should be just after ‘e‘
            {
                if( eindex != i-1) return false;
            }
            else if(!digits.Contains(s[i])) return false;
            else numindex = i;
        }
        //check whether we have extra spaces in the end
        if(i != size)
        {
            while(i<size)
            {
            if(s[i] !=‘ ‘) return false;
            i++;
            }
        }//blank at end
        if((dotindex==0 && eindex == 1)) return false;// such as .e12;
        if(numindex < 0 || numindex < eindex) return false;// such as . , e;
        return true;
    }
时间: 2024-10-17 18:25:16

65. Valid Number的相关文章

LeetCode --- 65. Valid Number

题目链接:Valid Number 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 statemen

No.65 Valid Number

No.65 Valid Number 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

leetCode 65.Valid Number (有效数字)

Valid Number 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

65. Valid Number *HARD*

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

Leetcode 65. Valid Number 验证数字 解题报告

1 解题思想 更新下:这道突然就很多访问,想起来好像是lt提交通过率最低的,嗯,我写的也不是特别详细,如有问题可以新浪微博@MebiuW交流~~ 这道题条条框框是在太多了,各种情况..不过简略来说,正确的做法应该是: 1.熟悉数字的表述规则(可以看网上的,也可以看我代码的),这道题关键是要懂所有的数字规则. 2.对输入的数字首先进行必要的检测,是否有abc或者中间空格等非法字符 3.将e前面和e后面分开计算!e前面允许有小数点形式的,e后面不允许有小数点形式的 4.数字的形式一般是 可以有正负号

[leedcode 65] Valid Number

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

[LeetCode 65] Valid Number (通过率最低的一题)

题目链接:valid-number /** * 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 st

[LeetCode] 65. Valid Number Java

题目: 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.

LeetCode:Valid Number - 判断字符串中内容是否为数字

1.题目名称 Valid Number(判断字符串中内容是否为数字) 2.题目地址 https://leetcode.com/problems/valid-number/ 3.题目内容 英文:Validate if a given string is numeric. 中文:给出一个字符串,检查这个字符串中内容是否是一个数字 例如:"0"." 0.1"."2e10"是数字,"abc"."1 a"不是数字 4