請你來實現一個?atoi?函數程梦,使其能將字符串轉換成整數岂贩。
首先拼缝,該函數會根據需要丟棄無用的開頭空格字符,直到尋找到第一個非空格的字符為止了嚎。
當我們尋找到的第一個非空字符為正或者負號時硝训,則將該符號與之后面盡可能多的連續(xù)數字組合起來,作為該整數的正負號新思;假如第一個非空字符是數字窖梁,則直接將其與之后連續(xù)的數字字符組合起來,形成整數夹囚。
該字符串除了有效的整數部分之后也可能會存在多余的字符纵刘,這些字符可以被忽略,它們對于函數不應該造成影響荸哟。
注意:假如該字符串中的第一個非空格字符不是一個有效整數字符假哎、字符串為空或字符串僅包含空白字符時,則你的函數不需要進行轉換舵抹。
在任何情況下劣砍,若函數不能進行有效的轉換時,請返回 0迅腔。
說明:
假設我們的環(huán)境只能存儲 32 位大小的有符號整數靠娱,那么其數值范圍為?[?231,? 231?? 1]像云。如果數值超過這個范圍,請返回 ?INT_MAX (231?? 1) 或?INT_MIN (?231)
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/string-to-integer-atoi
我的實現:
class Solution {
? ? public int myAtoi(String str) {
? ? ? int lenth=str.length();
? ? ? ? if(lenth==0) return 0;
? ? ? ? int tag=1;
? ? ? ? Stack<Integer> st = new Stack<Integer>();
? ? ? ? int i=0;
? ? ? ? if(Character.isDigit(str.charAt(0))){
? ? ? ? ? ? if(lenth==1) return str.charAt(0)-'0';
? ? ? ? ? ? if(str.charAt(i)!='0'){
? ? ? ? ? ? st.push(str.charAt(0)-'0');}
? ? ? ? ? ? i=1;
? ? ? ? ? ? while(i<lenth&&Character.isDigit(str.charAt(i))){
? ? ? ? ? ? if(!(st.isEmpty()&&str.charAt(i)=='0')){
? ? ? ? ? ? ? ? st.push(str.charAt(i)-'0');
? ? ? ? ? ? }
? ? ? ? ? ? i++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ? ? else if(str.charAt(0)=='-'||str.charAt(0)=='+'){
? ? ? ? ? ? ? ? if(lenth==1) return 0;
? ? ? ? ? ? ? ? if(str.charAt(0)=='-') tag=-1;
? ? ? ? ? ? ? ? i=1;
? ? ? ? ? ? ? ? while(i<lenth&&Character.isDigit(str.charAt(i))){
? ? ? ? ? ? ? ? if(!(st.isEmpty()&&str.charAt(i)=='0')){
? ? ? ? ? ? ? ? ? ? ? ? st.push(str.charAt(i)-'0');
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else if(str.charAt(0)==' '){
? ? ? ? ? ? ? ? if(lenth==1) return 0;
? ? ? ? ? ? ? ? i=1;
? ? ? ? ? ? ? ? while(i<lenth&&str.charAt(i)==' ') {i++;}
? ? ? ? ? ? ? ? if(i==lenth) return 0;
? ? ? ? ? ? ? ? if(str.charAt(i)=='-'){tag=-1;i++;}
? ? ? ? ? ? ? ? if(str.charAt(i)=='+'){i++;}
? ? ? ? ? ? ? ? while(i<lenth&&Character.isDigit(str.charAt(i))){
? ? ? ? ? ? ? ? if(!(st.isEmpty()&&str.charAt(i)=='0')){
? ? ? ? ? ? ? ? ? ? ? ? st.push(str.charAt(i)-'0');
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else{
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? }
? ? ? ? ? ? long num=0;
? ? ? ? ? ? int a=0;
? ? ? ? ? ? int temp=0;
? ? ? ? ? ? int tt=0;
? ? ? ? ? ? while(! st.isEmpty()){
? ? ? ? ? ? ? ? a = (Integer) st.pop().intValue();
? ? ? ? ? ? ? ? num=num+a*(long)Math.pow(10,temp);
? ? ? ? ? ? ? ? tt++;
? ? ? ? ? ? ? ? if(tag==1){
? ? ? ? ? ? ? ? if (num>Integer.MAX_VALUE||tt>=11) return Integer.MAX_VALUE;}
? ? ? ? ? ? ? ? if(tag==-1){
? ? ? ? ? ? ? ? if (num*-1 < Integer.MIN_VALUE||tt>=11) return Integer.MIN_VALUE;}
? ? ? ? ? ? ? ? temp++;
? ? ? ? ? ? }
? ? ? ? ? ? return (int)num*tag;
? ? }
}
這道題走了n多坑闲礼,坑總結:?while(Character.isDigit(str.charAt(i)))->?while(i<lenth&&Character.isDigit(str.charAt(i)))
原因:沒有考慮到str.charAt(i)會越界的問題柬泽;
?? if (num>Integer.MAX_VALUE&&tt>=10)->?if (num>Integer.MAX_VALUE||tt>=11)
原因:在遇到20000000000000000的時候報錯嫁蛇,原因是當num有數的時候直接超出了long的可表示范圍;
?num=num+a*Math.pow(10,temp);->?num=num+a*(long)Math.pow(10,temp);
原因:Math.pow(10,temp)可能超過int存儲范圍第煮,數組會截取
沒有考慮-00000123456這種情況
知識點總結:
判斷str某個字符是不是數字抑党? Character.isDigit(str.charAt(i))
字符轉數字(char轉int方法):?str.charAt(i)-'0'
letcode精選方法:
python一行:
class Solution:
? ? def myAtoi(self, s: str) -> int:
? ? ? ? return max(min(int(*re.findall('^[\+\-]?\d+', s.lstrip())), 2**31 - 1), -2**31)
使用正則表達式:
^:匹配字符串開頭
[\+\-]:代表一個+字符或-字符
?:前面一個字符可有可無
\d:一個數字
+:前面一個字符的一個或多個
\D:一個非數字字符
*:前面一個字符的0個或多個
max(min(數字, 2**31 - 1), -2**31) 用來防止結果越界
作者:QQqun902025048
鏈接:https://leetcode-cn.com/problems/string-to-integer-atoi/solution/python-1xing-zheng-ze-biao-da-shi-by-knifezhu/
來源:力扣(LeetCode)