這道題之前面甲骨文的時(shí)候被問(wèn)過(guò)。當(dāng)時(shí)是不要求判斷正負(fù)號(hào)的谭网。
一般被問(wèn)到什么問(wèn)題,還是看心情赃春。自己面別人也一樣愉择。所以不要迷信題庫(kù)。
這里寫的是可以轉(zhuǎn)換正數(shù)和負(fù)數(shù)的织中。
如果數(shù)字中間有空格锥涕,則只轉(zhuǎn)換空格之前的部分。
會(huì)判斷無(wú)效的輸入(非數(shù)字).
需要注意的幾點(diǎn):
- char的 ‘0’狭吼, ‘1’层坠, ‘2’, ‘3’字符如何轉(zhuǎn)換成對(duì)應(yīng)的數(shù)字
char - '0' - 對(duì)于Max, Min的判斷刁笙, 這里只說(shuō)Max的破花,Min的在代碼里,思路差不多
如果 result 在 計(jì)算下一位的時(shí)候已經(jīng)比Max的1/10要大疲吸,則說(shuō)明輸入值已經(jīng)超出范圍座每。
如果result 在 計(jì)算下一位的時(shí)候與Max的1/10一樣大,則看下一個(gè)加入的數(shù)字是否不大于Max的最后一位摘悴。
/**
* Created by creat on 2018/7/19.
* If there is space in the Integer, return the first part
* Can print and validate both Positive and Negative numbers
* Get the result in One loop. startOfInput -> startPosition, startPosition -> endOfValidInput
*/
public class StringToInteger {
int IN_VALID_NUMBER_CODE = -1;
char SPACE = ' ';
int TENTH_OF_MAX_INT = Integer.MAX_VALUE / 10;
int LAST_DIGIT_OF_MAX_INT = Integer.MAX_VALUE % 10;
int TENTH_OF_MIN_INT = Integer.MIN_VALUE / 10;
int LAST_DIGIT_OF_MIN_INT = 0 - Integer.MIN_VALUE % 10;
public static void main(String[] args) {
StringToInteger sti = new StringToInteger();
System.out.println("Max Int = " + Integer.MAX_VALUE + " And Min Int = " + Integer.MIN_VALUE);
System.out.println(sti.convertStringToInteger("1234")); // happy path positive
System.out.println(sti.convertStringToInteger("-1")); // happy path negative
System.out.println(sti.convertStringToInteger("2147483647")); // happy path max int
System.out.println(sti.convertStringToInteger("+2147483647")); // happy path max int with sign
System.out.println(sti.convertStringToInteger("-2147483648")); // happy path min int
System.out.println(sti.convertStringToInteger("2147483648")); // too big
System.out.println(sti.convertStringToInteger("-2147483649")); // too small
System.out.println(sti.convertStringToInteger(" +2147483647 ")); // happy path max int and space
System.out.println(sti.convertStringToInteger(" -2147483648 ")); // happy path min int and space
System.out.println(sti.convertStringToInteger(" -214 7483648 ")); // happy path min int and space, take the valid part before space
// below are invalid input tests
System.out.println("***************** Below are Invalid Input tests***************");
System.out.println(sti.convertStringToInteger("ab123cd"));
System.out.println(sti.convertStringToInteger("+0123"));
System.out.println(sti.convertStringToInteger("-0123"));
System.out.println(sti.convertStringToInteger("-123abc123"));
}
public int convertStringToInteger(String str) {
if (str == null || str.isEmpty()) {
System.out.println("No Input");
return IN_VALID_NUMBER_CODE;
}
int result = 0;
int beginPosition = -1;
int firstNumberPosition = -1;
char[] contents = str.toCharArray();
for (int i = 0; i < contents.length; i++) {
if (contents[i] == SPACE) {
continue;
}
if (isSign(contents[i]) || isDigit(contents[i])) {
beginPosition = i;
break;
}
}
if (beginPosition == IN_VALID_NUMBER_CODE) {
System.out.print("Invalid Input");
return IN_VALID_NUMBER_CODE;
}
if (isSign(contents[beginPosition])) {
firstNumberPosition = beginPosition + 1;
} else {
firstNumberPosition = beginPosition;
}
if (contents[firstNumberPosition] == '0') {
System.out.print("Invalid Input");
return IN_VALID_NUMBER_CODE;
}
for (int j = firstNumberPosition; j < contents.length; j++) {
if (contents[j] == SPACE) {
return result;
}
int digit = getDigit(contents[j]);
if (digit == IN_VALID_NUMBER_CODE) {
System.out.print("Invalid Input");
return IN_VALID_NUMBER_CODE;
}
if (isPositive(contents[beginPosition]) && result > TENTH_OF_MAX_INT) {
System.out.print("The input number is too large");
return IN_VALID_NUMBER_CODE;
}
if (isPositive(contents[beginPosition]) && result == TENTH_OF_MAX_INT && digit > LAST_DIGIT_OF_MAX_INT) {
System.out.print("The input number is too large");
return IN_VALID_NUMBER_CODE;
}
if (!isPositive(contents[beginPosition]) && result > 0 - TENTH_OF_MIN_INT) {
System.out.print("The input number is too small");
return IN_VALID_NUMBER_CODE;
}
if (!isPositive(contents[beginPosition]) && result == 0 - TENTH_OF_MIN_INT && digit > LAST_DIGIT_OF_MIN_INT) {
System.out.print("The input number is too small");
return IN_VALID_NUMBER_CODE;
}
result *= 10;
result += digit;
}
return isPositive(contents[beginPosition]) ? result : 0 - result;
}
private boolean isPositive(char c) {
return c == '+' || isDigit(c);
}
private int getDigit(char c) {
if (isDigit(c)) {
return c - '0';
} else {
return IN_VALID_NUMBER_CODE;
}
}
private boolean isDigit (char c) {
return c >= '0' && c <= '9';
}
private boolean isSign(char c) {
return c == '+' || c == '-';
}
}