成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 8. String to Integer (atoi)

cuieney / 1001人閱讀

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" => 1

Solution
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

相關(guān)文章

  • Leetcode 8 String to Integer (atoi)

    摘要:難度是標(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 ...

    cod7ce 評(píng)論0 收藏0
  • [Leetcode] String to Integer (atoi) 字符串轉(zhuǎn)整數(shù)

    摘要:通用方法復(fù)雜度時(shí)間空間思路字符串題一般考查的都是邊界條件特殊情況的處理。所以遇到此題一定要問清楚各種條件下的輸入輸出應(yīng)該是什么樣的。 String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input...

    Astrian 評(píng)論0 收藏0
  • July 算法習(xí)題 - 字符串2 + Leetcode 8,9

    摘要:判斷一條單向鏈表是不是回文解法可以借助棧,將遍歷到的前半段鏈表節(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)換成整...

    timger 評(píng)論0 收藏0
  • LeetCode 之 JavaScript 解答第8題 —— 字符串轉(zhuǎn)換整數(shù) (String to

    摘要:當(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(字...

    zhisheng 評(píng)論0 收藏0
  • 實(shí)現(xiàn)atoi函數(shù)(string轉(zhuǎn)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 ...

    leanote 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<