java 65.有效数字(第1个).java

Posted

tags:

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

# DFA : deterministic finite automaton

class Solution(object):
  def isNumber(self, s):
      """
      :type s: str
      :rtype: bool
      """
      #define a DFA
      state = [{}, 
              {'blank': 1, 'sign': 2, 'digit':3, '.':4}, 
              {'digit':3, '.':4},
              {'digit':3, '.':5, 'e':6, 'blank':9},
              {'digit':5},
              {'digit':5, 'e':6, 'blank':9},
              {'sign':7, 'digit':8},
              {'digit':8},
              {'digit':8, 'blank':9},
              {'blank':9}]
      currentState = 1
      for c in s:
          if c >= '0' and c <= '9':
              c = 'digit'
          if c == ' ':
              c = 'blank'
          if c in ['+', '-']:
              c = 'sign'
          if c not in state[currentState].keys():
              return False
          currentState = state[currentState][c]
      if currentState not in [3,5,8,9]:
          return False
      return True
    
    
"""
<img width="816" alt="screen shot 2017-06-29 at 7 59 06 pm" src="https://user-images.githubusercontent.com/22584778/27719173-710d1c56-5d05-11e7-993f-c4654828fd72.png">
"""
public class Solution {
    public boolean isNumber(String s) {
        //return s.matches("[-+]?(([0-9]+(.[0-9]*)?)|.[0-9]+)(e[-+]?[0-9]+)?");
        //return s.matches("[-+]?\\d*[.]?\\d+(e[-+]?\\d+)?"); // "1 " " 1 " "3."  
        //return s.matches("\\s*[-+]?\\d*[.]?\\d*(e[-+]?\\d+)?\\s*"); // "."
        return s.matches("\\s*[-+]?((\\d*[.]?\\d+)|(\\d+[.]?\\d*))(e[-+]?\\d+)?\\s*");
    }
}


import java.util.regex.*;

public class Solution {
    public boolean isNumber(String s) {
        s = s.trim();
        if(Pattern.matches("([-+])?((\\d+[.]\\d*)|(\\d*[.]\\d+)|(\\d+))([e]([-+])?\\d+)?",s)) {
            return true;
        }
        return false;
    }
}


/*
"." false
"3"
"00.192"
"123.567"
"-123.89"
"123E10" false
"123e10"
"123e-10"
"1 "
" 1 "
"3."
"-3."
"-.3333"
*/
public class Solution {
    public boolean isNumber(String s) {
    s = s.trim();
    
    boolean pointSeen = false;
    boolean eSeen = false;
    boolean numberSeen = false;
    boolean numberAfterE = true;
    for(int i=0; i<s.length(); i++) {
        if('0' <= s.charAt(i) && s.charAt(i) <= '9') {
            numberSeen = true;
            numberAfterE = true;
        } else if(s.charAt(i) == '.') {
            if(eSeen || pointSeen) {
                return false;
            }
            pointSeen = true;
        } else if(s.charAt(i) == 'e') {
            if(eSeen || !numberSeen) {
                return false;
            }
            numberAfterE = false;
            eSeen = true;
        } else if(s.charAt(i) == '-' || s.charAt(i) == '+') {
            if(i != 0 && s.charAt(i-1) != 'e') {
                return false;
            }
        } else {
            return false;
        }
    }
    
    return numberSeen && numberAfterE;
}
}

以上是关于java 65.有效数字(第1个).java的主要内容,如果未能解决你的问题,请参考以下文章

java 65.有效数字(第1个).java

java 65.有效数字(第1个).java

java 65.有效数字(第1个).java

java 65.有效数字(第1个).java

编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议65~69)

转载--编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议65~69)