Leetcode #65 Solution

    xiaoxiao2022-06-28  43

    Problem Description

    Validate if a given string is numeric.Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => trueNote: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

    Some Details

    ‘numeric’ is the key of this problem, also the reason of it being the most difficult problem to solve in leetcode.You need to make a list before coding, about how to define the number.

    Solution

    I finish this problem by making this list:

    Different bases. Your code need to distinguish data in binary, octal, decimal and hex formats.Scientific notation. ‘2e10’ as an example to show it. pic below. Space. Ignore if the string has space before the number of after the number.Symbol. ‘+”-’ is only allowed once before the number(twice in scientific notation because 2 numbers in scientific notation in fact).Decimal point. ‘.’ is only allowed once in the middle of the number.At least one digit. For example, ‘+’ , ‘.’ is not a number but ‘+1’ , ‘.0’ is OK.

    In fact, this list is more stringent than the requirements of the problem.

    Bases are unnecessary to discuss in this problem. Specifically, format in binary system and octal system is similar to it is in decimal system. What’s more, ‘abc’ as an example to show hex format is not allowed in the problem. The problem doesn’t follow strict rules in Scientific notation. Look at the pic below, if you judge whether A is in range of [1,10), you can’t pass this problem.

    It’s not really easy to accept this problem, so you could find more details in my code. Here are many hints in Chinese in my code.

    Code

    class Solution { public: bool binary(string s,int l,int r) { return false; //和十进制有差别吗? } bool oct(string s,int l,int r) { return false; //和十进制有差别吗? } bool dec(string s,int l,int r) { //十进制 bool p=false; int pn=-1; //小数点有吗 for (int i=l;i<=r;i++) { if (s[i]=='.') { if (!p) { p=true; pn=i; } else return false; } } bool eq=false;//这一部分合法吗? //什么叫合法? 至少有一位数字 至多有一个符号 for (int i=l;i<=r;i++) { if (s[i]>='0' && s[i]<='9') { eq=true; } else //不是数字 { if (i==l && (s[i]=='+' || s[i]=='-')); else if (p && i==pn && s[i]=='.'); else return false; //不是第一位出现的正负号 中间的小数点 就不合法 } } //允许有忽略前后置0的. return eq;//可能存在只有符号 } bool hex(string s,int l,int r) { return false; //十六进制,前面可能有0x,数字为0-9 A-F //样例 abc为false 先不考虑十六进制 } bool sci(string s,int l,int r) { //科学计数法 aeb=a*10^b 其中 1<=|a|<10 b整数 //0e1居然是true?? leetcode有点智障 bool e=false; int en=-1; for (int i=l;i<=r;i++) { if (s[i]=='e') { if (!e) { e=true; en=i; } else return false; } } if (!e) return false;//没e if (l==en || en==r) return false;//第一位最后一位e if (!dec(s,l,en-1)) return false;//前半部分不是数 int pn=en; for (int i=l;i<en;i++) if (s[i]=='.') { pn=i; break; } int num=0,now=1; bool eq=false; for (int i=pn-1;i>=l;i--) {//e前面部分的整数部分是不是[1,10) if (s[i]>='0' && s[i]<='9') { eq=true; num=num+now*(s[i]-'0'); now*=10; } else; } //暂且放过0 //暂且放过大于10 //if (num==0 || num>=10) return false; eq=false; for (int i=en+1;i<=r;i++) {//后半部分是不是整数 if (s[i]>='0' && s[i]<='9') { eq=true; } else { if (i==en+1 &&(s[i]=='+'||s[i]=='-')); else return false; } } return eq; } bool isNumber(string s) { int len=s.length(),l=0,r=len-1; while (s[l]==' ' && l<len) l++; while (s[r]==' ' && r>=0) r--; if (l>r) return false; if (binary(s,l,r)) return true; else if (oct(s,l,r)) return true; else if (dec(s,l,r)) return true; else if (hex(s,l,r)) return true; else if (sci(s,l,r)) return true; else return false; } };
    转载请注明原文地址: https://ju.6miu.com/read-1124405.html

    最新回复(0)