表示数值的字符串

题目描述

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
 
 1 public class Solution {
 2     public boolean check_help(char []str, int l, int r, int op) {
 3         if (l > r) return false;
 4         int num = 0;
 5         int point = 0;
 6         for (int i = l; i <= r; ++i) {
 7             if (str[i] >= '0' && str[i] <= '9') {
 8                 num++;
 9             }
10             if (str[i] == '.') point++;
11         }
12         
13         if (point <= op && num > 0 && num + point == r - l + 1 && str[r] != '.') {
14             return true;
15             
16         } else {
17             return false;
18         }
19     }
20     public boolean check(char []str, int l, int r, int op) {
21         if (l > r) return false;
22         if (str[l] == '+' || str[l] == '-') {
23             if (l + 1 <= r){
24                 if (check_help(str, l + 1, r, op)) {
25                     return true;
26                 } else {
27                     return false;
28                 }
29             } else {
30                 return false;
31             }
32         } else  {
33             if (check_help(str, l, r, op)) {
34                 return true;
35             } else {
36                 return false;
37             }
38         } 
39         
40        
41     }
42     public boolean isNumeric(char[] str) {
43         int n = str.length;
44         if (n == 0) return false;
45         if (str[0] == '+' || str[0] == '-' || (str[0] >= '0' && str[0] <= '9')) {
46             int i;
47             for (i = 0; i < n && str[i] != 'e' && str[i] != 'E'; ++i);
48             if (i < n) {
49                 if (check(str, 0, i - 1, 1) && check(str, i + 1, n - 1, 0)){
50                     return true;
51                 } else {
52                     return false;
53                 }
54             } else {
55                 if (check(str, 0, n - 1, 1)) return true;
56                 else return false;
57             }
58         } else {
59             return false;
60         }
61        
62     }
63 }
原文地址:https://www.cnblogs.com/hyxsolitude/p/12318614.html