|
| 1 | +packagehard; |
| 2 | +/**65. Valid Number QuestionEditorial Solution My Submissions |
| 3 | +Total Accepted: 50950 |
| 4 | +Total Submissions: 413050 |
| 5 | +Difficulty: Hard |
| 6 | +Validate if a given string is numeric. |
| 7 | +
|
| 8 | +Some examples: |
| 9 | +"0" => true |
| 10 | +" 0.1 " => true |
| 11 | +"abc" => false |
| 12 | +"1 a" => false |
| 13 | +"2e10" => true |
| 14 | +Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.*/ |
| 15 | +publicclassValidNumber { |
| 16 | +//strip off all leading whitespaces until encounter the first number or period |
| 17 | +//after that, only one 'e' is allowed and one '.' is allowed |
| 18 | +//also, this string could be negative, don't miss this case |
| 19 | +publicbooleanisNumber(Strings) { |
| 20 | +s =s.trim(); |
| 21 | +if (s.isEmpty()) |
| 22 | +returnfalse; |
| 23 | +inteCount =0,periodCount =0,index =0,numberCount =0; |
| 24 | +while(index <s.length()) { |
| 25 | +if(s.charAt(index) =='.')periodCount++; |
| 26 | +if((s.charAt(index) =='-') ||s.charAt(index) =='+' ||s.charAt(index) =='.')index++; |
| 27 | +if(periodCount >=2)returnfalse; |
| 28 | +elsebreak; |
| 29 | + } |
| 30 | +if(index >=s.length())returnfalse; |
| 31 | +while (index <s.length()) { |
| 32 | +if ((Character.getNumericValue(s.charAt(index)) <10 &&Character.getNumericValue(s |
| 33 | + .charAt(index)) >=0)) { |
| 34 | +index++; |
| 35 | +numberCount++; |
| 36 | +continue; |
| 37 | + }elseif (s.charAt(index) =='e') { |
| 38 | +if(eCount >1 ||numberCount ==0)returnfalse; |
| 39 | +if (eCount <2 &&index !=0 &&index != (s.length() -1)) { |
| 40 | +eCount++; |
| 41 | + }elseif (index == (s.length() -1) ||index ==0) |
| 42 | +returnfalse; |
| 43 | +if(eCount >1)returnfalse; |
| 44 | +index++; |
| 45 | +//after 'e', there could be '+' or '-' as long as there are numbers after these two signs |
| 46 | +if(index <s.length() && (s.charAt(index) =='+' ||s.charAt(index) =='-')) { |
| 47 | +index++; |
| 48 | +if(index >=s.length())returnfalse; |
| 49 | +elsecontinue; |
| 50 | + } |
| 51 | + }elseif (s.charAt(index) =='.') { |
| 52 | +if(eCount >=1)returnfalse; |
| 53 | +if(index-1 >=0 && (Character.getNumericValue(s.charAt(index-1)) >=10 ||Character.getNumericValue(s |
| 54 | + .charAt(index-1)) <0)){ |
| 55 | +if(s.charAt(index-1) =='+' ||s.charAt(index-1) =='-') { |
| 56 | +index++; |
| 57 | +continue; |
| 58 | + } |
| 59 | +elsereturnfalse; |
| 60 | + } |
| 61 | +if(index+1 <s.length() && (Character.getNumericValue(s.charAt(index+1)) >=10 ||Character.getNumericValue(s |
| 62 | + .charAt(index+1)) <0)){ |
| 63 | +if(s.charAt(index+1) =='e'){ |
| 64 | +index++; |
| 65 | +continue; |
| 66 | + } |
| 67 | +returnfalse; |
| 68 | + } |
| 69 | +if (periodCount <2 && (index +1 <= (s.length() -1)) ||index -1 >=0) { |
| 70 | +index++; |
| 71 | +periodCount++; |
| 72 | + } |
| 73 | +if (periodCount >=2 || (index ==0 &&index +1 >=s.length())) |
| 74 | +returnfalse; |
| 75 | + }else |
| 76 | +returnfalse; |
| 77 | + } |
| 78 | +returnnumberCount !=0; |
| 79 | + } |
| 80 | + |
| 81 | +publicstaticvoidmain(String...strings){ |
| 82 | +ValidNumbertest =newValidNumber(); |
| 83 | +// String s = "1 a"; |
| 84 | +// String s = "2e10"; |
| 85 | +// String s = "abc"; |
| 86 | +// String s = " 0.1 "; |
| 87 | +// String s = "0"; |
| 88 | +// String s = "3."; |
| 89 | +// String s = "0e"; |
| 90 | +// String s = "e9"; |
| 91 | +// String s = ".."; |
| 92 | +// String s = "."; |
| 93 | +// String s = " -.";//should be false |
| 94 | +// String s = ".e1"; |
| 95 | +// String s = "1e."; |
| 96 | +// String s = "-1."; |
| 97 | +// String s = "+++"; |
| 98 | +// String s = "3"; |
| 99 | +// String s = "+.8";//should be true |
| 100 | +// String s = "46.e3";//should be true |
| 101 | +// String s = "6e6.5";//should be false, i.e. after e, there should be no period |
| 102 | +// String s = "6ee69";//should be false |
| 103 | +// String s = ".e1";//should be false, i.e. there needs to be a number before 'e' appears? |
| 104 | +// String s = ".e10";//should this be true then? |
| 105 | +// String s = " 005047e+6"; |
| 106 | +Strings =" 4e+"; |
| 107 | +System.out.println(test.isNumber(s)); |
| 108 | + |
| 109 | +Integer.parseInt(s); |
| 110 | + } |
| 111 | +} |