1. 程式人生 > >65. Valid Number

65. Valid Number

points nbsp bsp https boolean .com string int leetcode

https://leetcode.com/problems/valid-number/discuss/23738/Clear-Java-solution-with-ifs

 1 class Solution {
 2     public boolean isNumber(String s) {
 3         s = s.trim();
 4         
 5         boolean numberSeen = false, numberAfterE = false, eSeen = false, pointSeen = false;
 6         for(int i = 0; i < s.length(); i++){
7 char c = s.charAt(i); 8 if(c >= ‘0‘ && c <= ‘9‘){ 9 numberSeen = true; 10 numberAfterE = true; 11 }else if(c == ‘.‘){ 12 if(pointSeen || eSeen){ 13 return false; 14 }
15 pointSeen = true; 16 }else if(c == ‘e‘){ 17 if(eSeen || !numberSeen){ 18 return false; 19 } 20 numberAfterE = false; 21 eSeen = true; 22 }else if(c == ‘+‘ || c == ‘-‘){ 23 if
(i != 0 && s.charAt(i-1) != ‘e‘){ 24 return false; 25 } 26 }else{ 27 return false; 28 } 29 } 30 return numberSeen && numberAfterE; 31 32 } 33 }

65. Valid Number