码迷,mamicode.com
首页 > 其他好文 > 详细

65. Valid Number

时间:2018-08-10 15:56:41      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:没有   位置   example   viewer   boolean   span   自己   div   char   

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

标签:没有   位置   example   viewer   boolean   span   自己   div   char   

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9454913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!