摘要:難度是標準庫中的一個函數可以將字符串表示的整數轉換為現在要求我們自己來實現它解題過程中主要有以下兩點需要注意字符串開頭可能出現或者需要處理使用來記錄中間結果防止溢出下面是的解法
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標準庫中的一個函數, 可以將字符串表示的整數轉換為int. 現在要求我們自己來實現它.
解題過程中, 主要有以下兩點需要注意:
字符串開頭可能出現 + 或者 - , 需要處理
使用long來記錄中間結果防止溢出
下面是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")); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/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...
摘要:通用方法復雜度時間空間思路字符串題一般考查的都是邊界條件特殊情況的處理。所以遇到此題一定要問清楚各種條件下的輸入輸出應該是什么樣的。 String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input...
摘要:判斷一條單向鏈表是不是回文解法可以借助棧,將遍歷到的前半段鏈表節點放入棧,后半段每當遍歷到一個,都要與出棧的節點相比較。如果中間出現不相等的情況,則不是回文。 [July 程序員編程藝術:面試和算法心得題目及習題][1] 字符串轉換成整數 also Leetcode 8 String to Integer (atoi) 題目描述 輸入一個由數字組成的字符串,把它轉換成整...
摘要:當我們尋找到的第一個非空字符為正或者負號時,則將該符號與之后面盡可能多的連續數字組合起來,作為該整數的正負號假如第一個非空字符是數字,則直接將其與之后連續的數字字符組合起來,形成整數。數字前正負號要保留。 Time:2019/4/19Title: String To IntegerDifficulty: MediumAuthor: 小鹿 題目:String To Integer(字...
摘要:實現函數轉思路利用內置的函數可以將字符串快速轉換成型利用是否拋出異常來快速判斷能否被轉換成,進而迅速確定輸入字符串中第一個非數字字符的位置需要注意處理符號的問題代碼如果是或者但是會拋出異常,此時返回由于的沒有取值上限,如果規定為 實現atoi函數(string轉integer) String to Integer (atoi) Implement atoi to convert a ...
閱讀 1504·2023-04-26 01:28
閱讀 3319·2021-11-22 13:53
閱讀 1431·2021-09-04 16:40
閱讀 3193·2019-08-30 15:55
閱讀 2685·2019-08-30 15:54
閱讀 2492·2019-08-30 13:47
閱讀 3372·2019-08-30 11:27
閱讀 1152·2019-08-29 13:21