【LeetCode】#8字串轉換整數(String to Integer)
【LeetCode】#8字串轉換整數(String to Integer)
題目描述
請你來實現一個 atoi 函式,使其能將字串轉換成整數。
首先,該函式會根據需要丟棄無用的開頭空格字元,直到尋找到第一個非空格的字元為止。
當我們尋找到的第一個非空字元為正或者負號時,則將該符號與之後面儘可能多的連續數字組合起來,作為該整數的正負號;假如第一個非空字元是數字,則直接將其與之後連續的數字字元組合起來,形成整數。
該字串除了有效的整數部分之後也可能會存在多餘的字元,這些字元可以被忽略,它們對於函式不應該造成影響。
注意:假如該字串中的第一個非空格字元不是一個有效整數字符、字串為空或字串僅包含空白字元時,則你的函式不需要進行轉換。
在任何情況下,若函式不能進行有效的轉換時,請返回 0。
假設我們的環境只能儲存 32 位大小的有符號整數,那麼其數值範圍為 [−231, 231 − 1]。如果數值超過這個範圍,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。
示例
示例 1:
輸入: “42”
輸出: 42
示例 2:
輸入: " -42"
輸出: -42
解釋: 第一個非空白字元為 ‘-’, 它是一個負號。
我們儘可能將負號與後面所有連續出現的數字組合起來,最後得到 -42 。
示例 3:
輸入: “4193 with words”
輸出: 4193
解釋: 轉換截止於數字 ‘3’ ,因為它的下一個字元不為數字。
示例 4:
輸入: “words and 987”
輸出: 0
解釋: 第一個非空字元是 ‘w’, 但它不是數字或正、負號。
因此無法執行有效的轉換。
示例 5:
輸入: “-91283472332”
輸出: -2147483648
解釋: 數字 “-91283472332” 超過 32 位有符號整數範圍。
因此返回 INT_MIN (−231) 。
Description
Implement atoi which converts a string to an integer.
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.
Note:
Only the space character ’ ’ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example
Example 1:
Input: “42”
Output: 42
Example 2:
Input: " -42"
Output: -42
Explanation: The first non-whitespace character is ‘-’, which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: “4193 with words”
Output: 4193
Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit.
Example 4:
Input: “words and 987”
Output: 0
Explanation: The first non-whitespace character is ‘w’, which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: “-91283472332”
Output: -2147483648
Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
解法
class Solution{
public int myAtoi(String str){
boolean flag = false;
boolean isZheng = true;
long res = 0;
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
if(c>='0' && c<='9')
res = res*10 + (c-'0');
else if(c=='+' && !flag)
isZheng = false;
else if(c=='-' && !flag)
isZheng = true;
else if(flag || !flag && c!=' ')
break;
if(!flag && c!=' '){
flag = true;
if(res-1 > Integer.MAX_VALUE)
break;
}
return isZheng ? (int)Math.min(res, Integer.MAX_VALUE)
: (int)Math.max(-1*res, Integer.MIN_VALUE);
}
}