65. Valid Number

Posted gopanama

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了65. Valid Number相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

65. Valid Number *HARD*

65. Valid Number

65. Valid Number

65. Valid Number

65. Valid Number

65.Valid Number