Leetcode 65. Valid Number Verification Digital problem Solving report

Source: Internet
Author: User

1 Thinking of solving problems

Update under: This is suddenly a lot of visits, think of it as if it was submitted through the lowest rate, well, I write is not particularly detailed, if there is a problem can Sina Weibo @mebiuw Exchange ~ ~

The rules are too many, various situations. But to be brief, the right approach should be:
1, familiar with the number of the expression rules (can see the internet, also can see my code), the key is to understand all the numerical rules.
2, to enter the number of the first necessary detection, whether there is ABC or the middle space and other illegal characters
3, the E front and E after the separate calculation! E is allowed in front of the decimal point, and E is not allowed after the decimal point form.
4, the number of the form generally can have a sign, if it is 0. A few, can omit 0, "." There is not necessarily a number in front, there must be a number behind the dot. Wait a minute

2 Original Questions

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.

Update (2015-02-10):
The signature of the C + + function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your cod E definition.

3 AC Solution
 Public  class solution {     Public Boolean Check(String tmp,BooleanPoint) {//For empty conditions must fail        if(Tmp.length () <1)return false;CharChars[]=tmp.tochararray ();intI=0;Booleannum=false;//Whether there is a symbol, number tenth should be skipped first, and there can only be one. If there is no number after the symbol, then it is illegal.        if(chars[i]==' + '|| chars[i]=='-') i++;if(i==chars.length)return false;//integer location data         while(I<chars.length && chars[i]<=' 9 '&& chars[i]>=' 0 ') {i++; num=true; }//If the decimal point is not allowed, then the process must match the end or it will fail        if(I<chars.length && point==false)return false;//Allow decimal point, next one must be a decimal point        if(I<chars.length && chars[i]=='. '){//decimal point, you must encounter e or end            BooleanNum2=false; i++; while(I<chars.length && chars[i]<=' 9 '&& chars[i]>=' 0 ') {i++; Num2=true; }//Before the decimal point must have the number, after the decimal point also must have the number, at least set up a            if(num2==false&& num==false)return false; }returnI==chars.length; } Public Boolean Isnumber(String s) {//Remove spaces         while(S.startswith (" ")) S=s.substring (1); while(S.endswith (" ")) S=s.substring (0, S.length ()-1);if(S.length () <1)return false;the decimal point is not allowed in the back of the//e, so if there is an e then the split operation        if(S.indexof (' E ')==-1)returnCheck (S,true);Else returnCheck (s.substring (0, S.indexof (' E ')),true) &&check (S.substring (S.indexof (' E ')+1),false) ; } Public Static void Main(String args[]) {Solution s=NewSolution (); System.out.println (S.isnumber (". 1 ")); }}

Leetcode 65. Valid Number Verification Digital problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.