Problem
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.
Example"10" => 10 "-1" => -1 "123123123123123" => 2147483647 "1.0" => 1Solution
public class Solution { public int myAtoi(String str) { str = str.trim(); boolean isNeg = false; int res = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (i == 0 && (ch == "+" || ch == "-")) isNeg = ch == "+" ? false: true; else if (ch >= "0" && ch <= "9") { int cur = ch - "0"; if (res > (Integer.MAX_VALUE-cur)/10) return isNeg ? Integer.MIN_VALUE: Integer.MAX_VALUE; else res = res*10+cur; } else return isNeg ? -res: res; } return isNeg? -res: res; } }Update 2018-10
class Solution { public int myAtoi(String str) { str = str.trim(); if (str == null || str.length() == 0) return 0; boolean isPositive = true; int index = 0; if (str.charAt(0) == "-") { isPositive = false; index++; } if (str.charAt(0) == "+") { index++; } int sum = 0; while (index < str.length()) { char ch = str.charAt(index); if (ch < "0" || ch > "9") break; int digit = str.charAt(index)-"0"; if ((Integer.MAX_VALUE-digit)/10 < sum) { return isPositive ? Integer.MAX_VALUE : Integer.MIN_VALUE; } sum = sum*10+digit; index++; } return isPositive ? sum : -sum; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65973.html
摘要:難度是標(biāo)準(zhǔn)庫(kù)中的一個(gè)函數(shù)可以將字符串表示的整數(shù)轉(zhuǎn)換為現(xiàn)在要求我們自己來實(shí)現(xiàn)它解題過程中主要有以下兩點(diǎn)需要注意字符串開頭可能出現(xiàn)或者需要處理使用來記錄中間結(jié)果防止溢出下面是的解法 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If ...
摘要:通用方法復(fù)雜度時(shí)間空間思路字符串題一般考查的都是邊界條件特殊情況的處理。所以遇到此題一定要問清楚各種條件下的輸入輸出應(yīng)該是什么樣的。 String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input...
摘要:判斷一條單向鏈表是不是回文解法可以借助棧,將遍歷到的前半段鏈表節(jié)點(diǎn)放入棧,后半段每當(dāng)遍歷到一個(gè),都要與出棧的節(jié)點(diǎn)相比較。如果中間出現(xiàn)不相等的情況,則不是回文。 [July 程序員編程藝術(shù):面試和算法心得題目及習(xí)題][1] 字符串轉(zhuǎn)換成整數(shù) also Leetcode 8 String to Integer (atoi) 題目描述 輸入一個(gè)由數(shù)字組成的字符串,把它轉(zhuǎn)換成整...
摘要:當(dāng)我們尋找到的第一個(gè)非空字符為正或者負(fù)號(hào)時(shí),則將該符號(hào)與之后面盡可能多的連續(xù)數(shù)字組合起來,作為該整數(shù)的正負(fù)號(hào)假如第一個(gè)非空字符是數(shù)字,則直接將其與之后連續(xù)的數(shù)字字符組合起來,形成整數(shù)。數(shù)字前正負(fù)號(hào)要保留。 Time:2019/4/19Title: String To IntegerDifficulty: MediumAuthor: 小鹿 題目:String To Integer(字...
摘要:實(shí)現(xiàn)函數(shù)轉(zhuǎn)思路利用內(nèi)置的函數(shù)可以將字符串快速轉(zhuǎn)換成型利用是否拋出異常來快速判斷能否被轉(zhuǎn)換成,進(jìn)而迅速確定輸入字符串中第一個(gè)非數(shù)字字符的位置需要注意處理符號(hào)的問題代碼如果是或者但是會(huì)拋出異常,此時(shí)返回由于的沒有取值上限,如果規(guī)定為 實(shí)現(xiàn)atoi函數(shù)(string轉(zhuǎn)integer) String to Integer (atoi) Implement atoi to convert a ...
閱讀 2825·2021-10-08 10:04
閱讀 3285·2021-09-10 11:20
閱讀 537·2019-08-30 10:54
閱讀 3334·2019-08-29 17:25
閱讀 2314·2019-08-29 16:24
閱讀 896·2019-08-29 12:26
閱讀 1456·2019-08-23 18:35
閱讀 1948·2019-08-23 17:53