標籤:leetcode java substring 字元轉換
String to Integer (atoi)
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
本文簡單點說就是把字串裡的數字轉成整形,但是題目有幾點要求:
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
翻譯大概意思是:
1、先丟棄字串前面的空白字元知道掃到第一個非空白字元;然後將一個合法的數值的字串以及其前面的一個加號或減號一同裝換為int;
2、該合法數值子字串中或後,可能包含其他非數值的特殊字元,忽略這些子字串;
3、若第一個非空子字串非護法的整型數值,或根本就沒有,甚至該字串可能都是特殊字元或Null 字元,則不作轉換0;
4、若沒有有效地轉換,則返回0,若有返回數值超出邊界,則返回邊界值;
一、思路: 本題雖然屬於比較簡單的層級,但是在沒考慮好全部可能的情況下,總是會出現遺漏一些細節的結果,導致提交後總是有新的意外情況出現。如:“ +-15651”、“ * 6644”這些首次出現的字串不能構成數位都是屬於不合法的情況,直接返回0。思路如下:先掃描到第一個非Null 字元,再考慮兩種情況,第一種是第一個非Null 字元是‘+‘或‘-‘號,若是則繼續判斷接下來的字元是否為數字,不是則不合法;另外一種是第一個非Null 字元是數字,那可繼續以掃描數位方式進行轉換。除了以上兩種情況,其他都為不合法。
二、Java程式
public class Solution { public int myAtoi(String str) { int sl = str.length(); int index=0; char tempc; //擷取字元 boolean flag = false; //負數標誌 int firstI=-1; long re=0; if(sl<=0) return 0; //1. 開始至第一個非Null 字元 while(str.charAt(index)==' ') { index++; if(index>=sl) //超過字串長度 return 0; } //2. 掃到第一個非Null 字元,判斷是否合法 tempc = str.charAt(index); //2.1 若是+-或數值則進行,否則則返回0 if(tempc=='-'||tempc=='+'||(tempc<='9'&&tempc>='0')) { //2.2 若是數值則跳出判斷 if((tempc<='9'&&tempc>='0')) { }else //2.3 若是+-號,則判斷接下來的是否為數值 { if(tempc=='-') flag = true;//2.3.1 若是-號,則設定標誌位 index++; //2.4 若是+-號,則判斷下一位是不是數值,是則合法,不是則返回0 tempc = str.charAt(index); if((tempc<='9'&&tempc>='0')) {}else { return 0; } } }else //2.1 若是+-或數值則進行,否則則跳出 { return 0; } //3. 開始掃描有效數字 while(index<sl) { tempc = str.charAt(index); if(tempc<='9'&&tempc>='0') { re = re*10 + (str.charAt(index)-'0'); if(re>=Integer.MAX_VALUE||re<=Integer.MIN_VALUE) break; }else { break; } index++; } if(flag==true) { re=-re; re = re<Integer.MIN_VALUE?Integer.MIN_VALUE:re; }else { re = re>Integer.MAX_VALUE?Integer.MAX_VALUE:re; } return (int)re; }}
LeetCode【8】. String to Integer (atoi) --java實現