摘要:難度是標(biāo)準(zhǔn)庫中的一個(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 you want a
challenge, please do not see below and ask yourself what are the
possible input cases.Notes: It is intended for this problem to be specified vaguely (ie, no
given input specs). You are responsible to gather all the input
requirements up front.Requirements for atoi: 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.
If the correct value is out of the range of representable values,
INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
難度: Easy
atoi是c標(biāo)準(zhǔn)庫中的一個(gè)函數(shù), 可以將字符串表示的整數(shù)轉(zhuǎn)換為int. 現(xiàn)在要求我們自己來實(shí)現(xiàn)它.
解題過程中, 主要有以下兩點(diǎn)需要注意:
字符串開頭可能出現(xiàn) + 或者 - , 需要處理
使用long來記錄中間結(jié)果防止溢出
下面是AC的解法:
public class Solution { public int myAtoi(String str) { str = str.trim(); if (str.length() == 0) { return 0; } int flag = 1; int start = 0; long sum = 0; if (str.charAt(0) == "-") { flag = -1; start = 1; } else if (str.charAt(0) == "+") { flag = 1; start = 1; } for (int i = start; i < str.length(); i++) { if (str.charAt(i) > "9" || str.charAt(i) < "0") { return this.getSuitInt(sum * flag); } sum = sum * 10 + (str.charAt(i) - "0"); if (sum >= Integer.MAX_VALUE) { break; } } return this.getSuitInt(sum * flag); } private int getSuitInt(long sum) { if (sum > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } else if (sum < Integer.MIN_VALUE) { return Integer.MIN_VALUE; } else { return (int) sum; } } public static void main(String[] args) { Solution s = new Solution(); System.out.println(s.myAtoi("0")); System.out.println(s.myAtoi("4")); System.out.println(s.myAtoi("18384")); System.out.println(s.myAtoi("-10203")); System.out.println(s.myAtoi("+304040")); System.out.println(s.myAtoi("9223372036854775809")); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66513.html
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...
摘要:通用方法復(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 ...
閱讀 1510·2023-04-26 01:28
閱讀 3325·2021-11-22 13:53
閱讀 1437·2021-09-04 16:40
閱讀 3198·2019-08-30 15:55
閱讀 2690·2019-08-30 15:54
閱讀 2498·2019-08-30 13:47
閱讀 3376·2019-08-30 11:27
閱讀 1157·2019-08-29 13:21