【題目描述】
Implement function atoi to convert a string to an integer.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.
實現atoi這個函數,將一個字符串轉換爲整數。如果沒有合法的整數,返回0。如果整數超出了32位整數的範圍,返回INT_MAX(2147483647)如果是正整數,或者INT_MIN(-2147483648)如果是負整數。
【題目鏈接】
www.lintcode.com/en/problem/string-to-integer-ii/
【題目解析】
經典的字符串轉整數題,邊界條件比較多,比如是否需要考慮小數點,空白及非法字符的處理,正負號的處理,科學計數法等。最先處理的是空白字符,然後是正負號,接下來只要出現非法字符(包含正負號,小數點等,無需對這兩類單獨處理)即退出,否則按照正負號的整數進位加法處理。
【參考答案】