[Leetcode] valid number

Source: Internet
Author: User

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 shoshould gather all requirements up front before implementing one.

Algorithm ideas:

Exclusion: If all illegal conditions occur, false is returned, and true is returned after scanning.

Illegal:

Symbol: +,-can appear only twice at most, and the second time must appear after E/E. In addition, there must be no number before the first plus sign (+/-), and the second must be followed by a number after E;

E: it can only appear once at most. Besides, numbers must be placed before and after each vertex;

.: It can only appear once at most, and cannot appear after E. If it is the last digit of a string, there must be a number before it;

[Post-Submission error supplement] at the first submission, "E" reports an error. The boundary is not considered.

In short, this question is not difficult, but tedious:

I hope you can learn from the following error cases: (a lot of cases)

005047e + 6 true

3. True

. 1 true

-. False

6 + 1 false

. 1 false

0e false

The Code is as follows:

 1 public class Solution { 2     public boolean isNumber(String s) { 3         if(s == null || s.trim().length() == 0) return false; 4         s = s.trim(); 5         char[] charArray = {‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘.‘,‘e‘,‘E‘,‘+‘,‘-‘}; 6         Set<Character> set = new HashSet<Character>(); 7         for(char c : charArray){ 8             set.add(c); 9         }10         boolean hasE = false, hasPoint = false, hasNum = false;11         int operaterCount = 0;12         for(int i = 0; i < s.length(); i++){13             if(!set.contains(s.charAt(i))) return false;14             if(s.charAt(i) <= ‘9‘ && s.charAt(i) >= ‘0‘) hasNum = true;15             if(s.charAt(i) == ‘E‘ || s.charAt(i) == ‘e‘){16                 if(i == 0 || i == s.length() - 1) return false;17                 if(!hasE && hasNum) hasE = true;18                 else return false;19             }20             if(s.charAt(i) == ‘.‘){21                 if(i == 0 && 1 == s.length()) return false;22                 if(i == s.length() - 1 && !hasNum) return false;23                 if(!hasPoint && !hasE) hasPoint = true;24                 else return false;25             }26             if(s.charAt(i) == ‘-‘ || s.charAt(i) == ‘+‘){27                 if(i == s.length() - 1 || (i != 0 && s.charAt(i - 1) != ‘e‘ && s.charAt(i - 1) != ‘E‘)) return false;28                 if(operaterCount == 2) return false;29                 if(operaterCount == 1 && !hasE && !hasNum) return false;30                 operaterCount++;31             }32         }33         return true;34     }35 }

 

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.