Leetcode: valid number

Source: Internet
Author: User

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.

This is a question that checks whether string input is valid. The basic rule is to use scientific notation, so there are several special characters: symbol bits '+', '-', decimal point '. ', and 'E'. Only the digits 0-9 are left. If other characters are invalid, false is returned. It is okay where numeric characters appear. We mainly consider a few special characters.
When the decimal point appears, we need to meet these conditions: (1) there cannot be a decimal point or 'e' or 'E'; (2) the first digit is a number (not the first digit) or the last digit is a number (not the last digit ).
In the case of positive and negative numbers, the conditions must be met: (1) must be the first digit or the last digit after 'E' and 'E'; (2) the last digit must be a number.
For 'E' and 'E', the following conditions must be met: (1) there must be no 'e' or 'e' before; (2) it cannot be the first digit (no digital scientific count is meaningless) or the last digit (no number is needed to write an index ).
Based on the situations listed above, we use two labels and the judgment of the front and back spaces. The algorithm complexity is obviously O (n), and only the extra space of O (1) is required. The Code is as follows:

The most difficult thing to understand is the 11th rows. To understand this, the correct way to use the decimal point is-apart from the number of decimal points or 'e' and 'E, the former or the latter has at least one number. The exact point is: (the former is a number & not the first) | (the last digit is a number & not the last digit ). In this example, we need to find the case with the decimal point error and return false. The condition with the decimal point error is that it does not meet the above conditions. Therefore, we need to reverse the above conditions, then we get the following result: (first digit | the first digit is not a number) & (last digit | the last digit is not a number). This is the condition where the decimal point is incorrect, that is

(I = 0 |! (S. charat (I-1)> = '0' & S. charat (I-1) <= '9') & (I = S. length ()-1 |! (S. charat (I + 1)> = '0' & S. charat (I + 1) <= '9 '))

 1 public class Solution { 2     public boolean isNumber(String s) { 3         if (s == null) return false; 4         s = s.trim(); 5         if (s.length() == 0) return false; 6         boolean dotFlag = false; 7         boolean eFlag = false; 8         for (int i=0; i<s.length(); i++) { 9             switch(s.charAt(i)) {10                 case ‘.‘: 11                     if (dotFlag || eFlag || (i==0 || !(s.charAt(i-1)>=‘0‘ && s.charAt(i-1)<=‘9‘)) && (i==s.length()-1 || !(s.charAt(i+1)>=‘0‘ && s.charAt(i+1)<=‘9‘))) {12                         return false;13                     }14                     dotFlag = true;15                     break;16                 case ‘+‘:17                 case ‘-‘:18                     if ((i>0 && (s.charAt(i-1)!=‘e‘) && s.charAt(i-1)!=‘E‘) || i==s.length()-1 || !(s.charAt(i+1)>=‘0‘ && s.charAt(i+1)<=‘9‘ || s.charAt(i+1)==‘.‘)) {19                         return false;20                     }21                     break;22                 case ‘E‘:23                 case ‘e‘:24                     if (eFlag || i==0 || i==s.length()-1) {25                         return false;26                     }27                     eFlag = true;28                     break;29                 case ‘0‘:30                 case ‘1‘:31                 case ‘2‘:32                 case ‘3‘:33                 case ‘4‘:34                 case ‘5‘:35                 case ‘6‘:36                 case ‘7‘:37                 case ‘8‘:38                 case ‘9‘:39                     break;40                 default: return false;41             }42         }43         return true;44     }45 }

 

Leetcode: valid number

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.