国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode-StringtoInteger(atoi)

2019-11-14 14:49:59
字體:
來源:轉載
供稿:網友

題目:

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.

思路:

基本運算;注意判斷乘和加的溢出。

package manipulation;public class StringToInteger {    public int myAtoi(String str) {        int len;        if (str == null || (len = str.length()) == 0) return 0;        int pos = 0;        int sign = 1;        int res = 0;        while (Character.isWhitespace(str.charAt(pos))) ++pos;        if (str.charAt(pos) == '+') {            ++pos;        } else if (str.charAt(pos) == '-') {            ++pos;            sign = -1;        }                while (pos < len && Character.isDigit(str.charAt(pos))){            int value = str.charAt(pos) - '0';            int preRes = res;
// We use Math.abs here because on the second time, res will become positive if sign == -1 res
= Math.abs(res) * 10 * sign; if (res/(10 * sign) != Math.abs(preRes)) { return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } preRes = res; res = res + sign * value; if (sign == 1 && res < preRes) return Integer.MAX_VALUE; if (sign == -1 && res > preRes) return Integer.MIN_VALUE; ++pos; } return res; } public static void main(String[] args) { // TODO Auto-generated method stub String str = "-123"; StringToInteger sti = new StringToInteger(); System.out.println(sti.myAtoi(str)); }}

代碼更簡潔一些:

package manipulation;public class StringToInteger {    public int myAtoi(String str) {        int len;        if (str == null || (len = str.length()) == 0) return 0;        int pos = 0;        int sign = 1;        int res = 0;        while (Character.isWhitespace(str.charAt(pos))) ++pos;        if (str.charAt(pos) == '+') {            ++pos;        } else if (str.charAt(pos) == '-') {            ++pos;            sign = -1;        }                while (pos < len && Character.isDigit(str.charAt(pos))) {            int value = str.charAt(pos) - '0';            if (sign == 1 && res > (Integer.MAX_VALUE - value) / 10)                     return Integer.MAX_VALUE;                                    if (sign == -1 && (-res < (Integer.MIN_VALUE + value) / 10))                     return Integer.MIN_VALUE;                                res = res * 10 + value;            ++pos;        }                return sign > 0 ? res : -res;    }        public static void main(String[] args) {        // TODO Auto-generated method stub        String str = "-2147483648";        StringToInteger sti = new StringToInteger();        System.out.println(sti.myAtoi(str));        // -(INT_MIN_VALUE) is still INT_MIN_VALUE         int x = -2147483648;        System.out.println(-x);    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄浦区| 鹤峰县| 新绛县| 清苑县| 岢岚县| 沅江市| 新余市| 杭锦旗| 阿鲁科尔沁旗| 伊宁县| 广汉市| 鄂伦春自治旗| 二手房| 晋江市| 城市| 枣强县| 明光市| 尚志市| 和硕县| 正镶白旗| 涟源市| 农安县| 洛阳市| 隆化县| 石台县| 铁岭市| 福泉市| 青海省| 岗巴县| 朝阳县| 岚皋县| 新干县| 治县。| 仙居县| 黑河市| 邢台县| 义乌市| 舞钢市| 广昌县| 溆浦县| 福清市|