劍指offer(五十五)之表示數值的字串
請實現一個函式用來判斷字串是否表示數值(包括整數和小數)。例如,字串"+100","5e2","-123","3.1416"和"-1E-16"都表示數值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
程式碼1:
程式碼2:public class Solution { public boolean isNumeric(char[] str) { String string = String.valueOf(str); return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?"); } }
<span style="color:#000099;">public class Solution {
public boolean isNumeric(char[] str) {
try {
double re = Double.parseDouble(new String(str));
} catch (NumberFormatException e) {
return false;
}
return true;
}
}</span>