【劍指Offer學習】【面試題54:表示數值的字串】
阿新 • • 發佈:2019-01-03
題目:請實現一個函式用來判斷字串是否表示數值(包括整數和小數)。
例子說明
例如,字串“+100”,“5e2”,“-123”,“3.1416”及”-1E-16”都表示數值,但“12e”,”1a3.14”,”1.2.3”,”+-5”及“12e+5.4”都不是。
解題思路
在數值之前可能有一個表示正負的’-‘或者’+’。接下來是若干個0到9的數位表示數值的整數部分(在某些小數裡可能沒有數值的整數部分)。如果數值是一個小數,那麼在小數點後面可能會有若干個0到9的數位表示數值的小數部分。如果數值用科學計數法表示,接下來是一個’e’或者‘E’,以及緊跟著的一個整數(可以有正負號)表示指數。
判斷一個字串是否符合上述模式時,首先看第一個字元是不是正負號。如果是,在字串上移動一個字元,繼續掃描剩餘的字串中0到9的數位。如果是一個小數,則將遇到小數點。另外,如果是用科學計數法表示的數值,在整數或者小數的後面還有可能遇到’e’或者’E’。
程式碼實現
public class Test54 {
/**
* 題目:請實現一個函式用來判斷字串是否表示數值(包括整數和小數)。
*
* @param string
* @return
*/
public static boolean isNumeric(String string) {
if (string == null || string.length() < 1) {
return false;
}
int index = 0;
if (string.charAt(index) == '+' || string.charAt(index) == '-') {
index++;
}
// 已經到達字串的末尾了
if (index >= string.length()) {
return false;
}
boolean numeric = true;
index = scanDigits(string, index);
// 還未到字串的末尾
if (index < string .length()) {
// 如果是小數點
if (string.charAt(index) == '.') {
// 移動到下一個位置
index++;
index = scanDigits(string, index);
// 已經到了字串的末尾了
if (index >= string.length()) {
numeric = true;
}
// 還未到字串結束位置
else if (index < string.length() && (string.charAt(index) == 'e' || string.charAt(index) == 'E')) {
numeric = isExponential(string, index);
} else {
numeric = false;
}
}
// 如果是指數標識
else if (string.charAt(index) == 'e' || string.charAt(index) == 'E') {
numeric = isExponential(string, index);
} else {
numeric = false;
}
return numeric;
}
// 已經到了字串的末尾了,說明其沒有指數部分
else {
return true;
}
}
/**
* 判斷是否是科學計數法的結尾部分,如E5,e5,E+5,e-5,e(E)後面接整數
*
* @param string 字串
* @param index 開始匹配的位置
* @return 匹配的結果
*/
private static boolean isExponential(String string, int index) {
if (index >= string.length() || (string.charAt(index) != 'e' && string.charAt(index) != 'E')) {
return false;
}
// 移動到下一個要處理的位置
index++;
// 到達字串的末尾,就返回false
if (index >= string.length()) {
return false;
}
if (string.charAt(index) == '+' || string.charAt(index) == '-') {
index++;
}
// 到達字串的末尾,就返回false
if (index >= string.length()) {
return false;
}
index = scanDigits(string, index);
// 如果已經處理到了的數字的末尾就認為是正確的指數
return index >= string.length();
}
/**
* 掃描字串部分的數字部分
*
* @param string 字串
* @param index 開始掃描的位置
* @return 從掃描位置開始第一個數字字元的位置
*/
private static int scanDigits(String string, int index) {
while (index < string.length() && string.charAt(index) >= '0' && string.charAt(index) <= '9') {
index++;
}
return index;
}
public static void main(String[] args) {
System.out.println(isNumeric("100") + "[" + true + "]");
System.out.println(isNumeric("123.45e+6") + "[" + true + "]");
System.out.println(isNumeric("+500") + "[" + true + "]");
System.out.println(isNumeric("5e2") + "[" + true + "]");
System.out.println(isNumeric("3.1416") + "[" + true + "]");
System.out.println(isNumeric("600.") + "[" + true + "]");
System.out.println(isNumeric("-.123") + "[" + true + "]");
System.out.println(isNumeric("-1E-16") + "[" + true + "]");
System.out.println(isNumeric("100") + "[" + true + "]");
System.out.println(isNumeric("1.79769313486232E+308") + "[" + true + "]");
System.out.println();
System.out.println(isNumeric("12e") + "[" + false + "]");
System.out.println(isNumeric("1a3.14") + "[" + false + "]");
System.out.println(isNumeric("1+23") + "[" + false + "]");
System.out.println(isNumeric("1.2.3") + "[" + false + "]");
System.out.println(isNumeric("+-5") + "[" + false + "]");
System.out.println(isNumeric("12e+5.4") + "[" + false + "]");
}
}