image.png
    这道题虽然我用ifelse 做出来了,但是正儿八经的正统算法还是需要自动机来完成的,
    包括电梯问题。

    我的代码

    1. public static void main(String[] args) {
    2. String s = " 1 11 ";
    3. System.out.println("===" +s.trim()+ "===");
    4. String ss = "-1E-16";
    5. System.out.println(isNumber(ss));
    6. }
    7. public static boolean isNumber(String s) {
    8. if(s == null || s.length() == 0) return false;
    9. boolean res = process(s.trim().toCharArray());
    10. return res;
    11. }
    12. public static boolean process(char[] chs){
    13. if(chs.length ==0) return false;
    14. if( chs.length ==1 && (chs[0] < '0' || chs[0]>'9') )return false;
    15. int e = -1;
    16. for(int i = 0; i<chs.length; i++){
    17. if(chs[i] =='e' || chs[i] =='E'){
    18. e = i;
    19. break;
    20. }
    21. }
    22. boolean res;
    23. if(e != -1){
    24. res = pointNum(chs, 0, e-1) && eNum(chs, e, chs.length-1);
    25. }else {
    26. res = pointNum(chs, 0, chs.length-1);
    27. }
    28. return res;
    29. }
    30. public static boolean posNum(char[] chs, int i , int j){
    31. if(i==j && (chs[i]<'0' || chs[i] >'9' )) return false;
    32. if(chs[i] != '+' && (chs[i]<'0' || chs[i]>'9')) {
    33. return false;
    34. }
    35. else {
    36. for(int index = i+1; index <=j; index ++){
    37. if(chs[index] > '9' || chs[index]<'0') return false;
    38. }
    39. }
    40. return true;
    41. }
    42. public static boolean negNum(char[] chs, int i, int j){
    43. if(i==j && (chs[i]<'0' || chs[i] >'9' )) return false;
    44. if(chs[i] != '-' && (chs[i]<'0' || chs[i]>'9')) {
    45. return false;
    46. }
    47. else {
    48. for(int index = i+1; index <= j ; index ++){
    49. if(chs[index] > '9' || chs[index]<'0') return false;
    50. }
    51. }
    52. return true;
    53. }
    54. public static boolean pointNum(char[] chs, int i , int j){
    55. if(i==j && (chs[i]<'0' || chs[i] >'9' )) return false;
    56. if(j-i ==1 && chs[j] =='.' && (chs[i]<'0' || chs[i] > '9')) return false;
    57. boolean point = false;
    58. if(chs[i] !='.' && chs[i] !='+' && chs[i]!='-' &&( chs[i]<'0' || chs[i]>'9')){
    59. return false;
    60. }
    61. if(chs[i] == '.'){
    62. point = true;
    63. }
    64. for(int index =i+1; index<=j; index++){
    65. if(!point && chs[index] != '.' && ( chs[index] <'0' || chs[index]> '9')){
    66. return false;
    67. }else if(point && chs[index] =='.'){
    68. return false;
    69. }else if(!point && chs[index] == '.') {
    70. point = true;
    71. }else if( chs[index] <'0' || chs[index]> '9'){
    72. return false;
    73. }
    74. }
    75. return true;
    76. }
    77. /**i 表示 e首次出现的位置*/
    78. public static boolean eNum(char[] chs, int i, int j){
    79. boolean hasE = false;
    80. if(i +1 == chs.length) return false;
    81. return posNum(chs,i+1,j) || negNum(chs, i+1, j );
    82. }

    官方题解:

    1. class Solution {
    2. public boolean isNumber(String s) {
    3. Map<State, Map<CharType, State>> transfer = new HashMap<State, Map<CharType, State>>();
    4. Map<CharType, State> initialMap = new HashMap<CharType, State>() {{
    5. put(CharType.CHAR_SPACE, State.STATE_INITIAL);
    6. put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
    7. put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
    8. put(CharType.CHAR_SIGN, State.STATE_INT_SIGN);
    9. }};
    10. transfer.put(State.STATE_INITIAL, initialMap);
    11. Map<CharType, State> intSignMap = new HashMap<CharType, State>() {{
    12. put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
    13. put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
    14. }};
    15. transfer.put(State.STATE_INT_SIGN, intSignMap);
    16. Map<CharType, State> integerMap = new HashMap<CharType, State>() {{
    17. put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
    18. put(CharType.CHAR_EXP, State.STATE_EXP);
    19. put(CharType.CHAR_POINT, State.STATE_POINT);
    20. put(CharType.CHAR_SPACE, State.STATE_END);
    21. }};
    22. transfer.put(State.STATE_INTEGER, integerMap);
    23. Map<CharType, State> pointMap = new HashMap<CharType, State>() {{
    24. put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
    25. put(CharType.CHAR_EXP, State.STATE_EXP);
    26. put(CharType.CHAR_SPACE, State.STATE_END);
    27. }};
    28. transfer.put(State.STATE_POINT, pointMap);
    29. Map<CharType, State> pointWithoutIntMap = new HashMap<CharType, State>() {{
    30. put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
    31. }};
    32. transfer.put(State.STATE_POINT_WITHOUT_INT, pointWithoutIntMap);
    33. Map<CharType, State> fractionMap = new HashMap<CharType, State>() {{
    34. put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
    35. put(CharType.CHAR_EXP, State.STATE_EXP);
    36. put(CharType.CHAR_SPACE, State.STATE_END);
    37. }};
    38. transfer.put(State.STATE_FRACTION, fractionMap);
    39. Map<CharType, State> expMap = new HashMap<CharType, State>() {{
    40. put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
    41. put(CharType.CHAR_SIGN, State.STATE_EXP_SIGN);
    42. }};
    43. transfer.put(State.STATE_EXP, expMap);
    44. Map<CharType, State> expSignMap = new HashMap<CharType, State>() {{
    45. put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
    46. }};
    47. transfer.put(State.STATE_EXP_SIGN, expSignMap);
    48. Map<CharType, State> expNumberMap = new HashMap<CharType, State>() {{
    49. put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
    50. put(CharType.CHAR_SPACE, State.STATE_END);
    51. }};
    52. transfer.put(State.STATE_EXP_NUMBER, expNumberMap);
    53. Map<CharType, State> endMap = new HashMap<CharType, State>() {{
    54. put(CharType.CHAR_SPACE, State.STATE_END);
    55. }};
    56. transfer.put(State.STATE_END, endMap);
    57. int length = s.length();
    58. State state = State.STATE_INITIAL;
    59. for (int i = 0; i < length; i++) {
    60. CharType type = toCharType(s.charAt(i));
    61. if (!transfer.get(state).containsKey(type)) {
    62. return false;
    63. } else {
    64. state = transfer.get(state).get(type);
    65. }
    66. }
    67. return state == State.STATE_INTEGER || state == State.STATE_POINT || state == State.STATE_FRACTION || state == State.STATE_EXP_NUMBER || state == State.STATE_END;
    68. }
    69. public CharType toCharType(char ch) {
    70. if (ch >= '0' && ch <= '9') {
    71. return CharType.CHAR_NUMBER;
    72. } else if (ch == 'e' || ch == 'E') {
    73. return CharType.CHAR_EXP;
    74. } else if (ch == '.') {
    75. return CharType.CHAR_POINT;
    76. } else if (ch == '+' || ch == '-') {
    77. return CharType.CHAR_SIGN;
    78. } else if (ch == ' ') {
    79. return CharType.CHAR_SPACE;
    80. } else {
    81. return CharType.CHAR_ILLEGAL;
    82. }
    83. }
    84. enum State {
    85. STATE_INITIAL,
    86. STATE_INT_SIGN,
    87. STATE_INTEGER,
    88. STATE_POINT,
    89. STATE_POINT_WITHOUT_INT,
    90. STATE_FRACTION,
    91. STATE_EXP,
    92. STATE_EXP_SIGN,
    93. STATE_EXP_NUMBER,
    94. STATE_END
    95. }
    96. enum CharType {
    97. CHAR_NUMBER,
    98. CHAR_EXP,
    99. CHAR_POINT,
    100. CHAR_SIGN,
    101. CHAR_SPACE,
    102. CHAR_ILLEGAL
    103. }
    104. }
    105. 作者:LeetCode-Solution
    106. 链接:https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/solution/biao-shi-shu-zhi-de-zi-fu-chuan-by-leetcode-soluti/
    107. 来源:力扣(LeetCode
    108. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。