65. Valid Number

Posted tobeabetterpig

tags:

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

65. Valid Number

https://www.youtube.com/watch?v=QXNvEz-GwQ4
这道题就是把这个string 分成一段一段的, 分别来判断每一段是不是合法。 有些 case 
需要和面试官讨论返回什么, 比如 3.  或者  .45

这个题参照视频中的思路, 自己写代码




empty space +-10.234e56 emptyspace

          
这道题就是让 i 走到 i 所能走到的有效位置。 
最后检查 i 的 长度 是否和 这个string 本身的
长度 相等, 如果 相等就说明 这个string 是 valid
如果不相等, 就 说明 这个 i 走到 某个 地方 走不动了

 It is intended for the problem statement to be ambiguous.
   You should gather all requirements up front before implementing one.
   
具体的细节规则根据面试官的 要求 来 判断 什么时候 可以
继续移动 i, 什么时候 i 不能 再 往后 走了 
      
      
      
           

Some examples:
"0" => true      i stops after 0, we covered the whole length and isDigit is true
" 0.1 " => true i stops after 1, we covered the whole length and isDigit is true
"abc" => false  no letters besides e, i stops at 0 
"1 a" => false there must not be a letter besides e, i stops before a  
"2e10" => true  there must be a number right after e   
 "2e" false      i stops at index = 2, so we covered the full length
                 but we failed at isDigit as there must be a number after e 
      


cases need to ask the interviewer for opinions on the rule 
"10.e56"   
"3.0"
"0.2"
".2"  
"3."

  
      
  "10.e56"
  // if this is valid, then its okay for not 
  
  
  
"3.0"
"0.2"
".2"  
"3."
  




没有通过

class Solution {
    public boolean isNumber(String s) {
      if(s == null || s.length() == 0){
        return false;
      }
      
      int i = 0;
      int n = s.length();
      //space
      while(i < n && Character.isWhitespace(s.charAt(i))){
        i++;
      }
      // check + - sign 
      if(s.charAt(i) == ‘+‘ || s.charAt(i) == ‘-‘){
        i++;
      }
      
      // check digit
      boolean isDigit = false;
      while(i < n && Character.isDigit(s.charAt(i))){
        i++;
        isDigit = true;
      }
      
      //check dot 
      if(i < n && s.charAt(i) == ‘.‘){
        i++;
        // there must be digits before e, so if the e is after dot
        // we need to check if there are digits after the dot before the e 
        //isDigit = false;
        // if 3. is valid, then its okay not to have digit after the dot
        // if 3. is not valid, then we must have isDigit = false here
        
        // if 3.e12 is not valid , we must have isDigit = false here . because for 
        // checking digits before e after the dot 
        
        // .12e34 is also valid , in case we dont have numbers before the dot 
        // all this means there must be numbers before e, no matter 
        // its before the dot, after the dot. 
        while(i < n && Character.isDigit(s.charAt(i))){
          i++;
          isDigit = true;
        }
      }
      
      // 12e34 is valid, 12.e34 is also valid , .12e34 is also valid 
      if(i < n && s.charAt(i) == ‘e‘ && isDigit){
        i++;
        // there must be digits after e. so here we set isDigit = false
        isDigit = false;
        // there could also be + - sign
        if(i < n && (s.charAt(i) == ‘+‘ || s.charAt(i) == ‘-‘)){
          i++;
        }
        // checking the number 
        while (i < n && Character.isDigit(s.charAt(i))){
          i++;
          isDigit = true;
        }
      }
      
      // check whiteSpace
      while(i < n && Character.isWhitespace(s.charAt(i))){
        i++;
      }
      
      // end 
      // if there is supposed to have digits , but there is none , return false
      if(isDigit == false){
        return false;
      }
      // if the i stops before it reaches the very end of the string, return false
      if(i != s.length()){
        return false;
      }
      return true;
    }
}

 

以上是关于65. Valid Number的主要内容,如果未能解决你的问题,请参考以下文章

65. Valid Number *HARD*

65. Valid Number

65. Valid Number

65. Valid Number

65. Valid Number

65.Valid Number