首页 > 代码库 > leetcode string to integer

leetcode string to integer

 

1, string.trim()将string中的空格去掉。

2,java 中有定义int的最大值和最小值,Integer.MAX_VALUE  Integer.MIN_VALUE

3,将char的值转换为integer的值, 就-‘0’

 

 

public class stringtoint {    public int atoi(String str) {        final int max = Integer.MAX_VALUE;        final int min = Integer.MIN_VALUE;        if (str == null) {            return 0;        }        str = str.trim();        if (str.length() == 0) {            return 0;        }        int sign = 1;        int i = 0;        if (str.charAt(0) == ‘+‘) {            sign = 1;            i++;        }        if (str.charAt(0) == ‘-‘) {            sign = -1;            i++;        }        long temp = 0;        for (; i < str.length(); i++) {            if (str.charAt(i) < ‘0‘ || str.charAt(i) > ‘9‘) {                break;            }            temp = temp * 10 + str.charAt(i) - ‘0‘;            if (temp > max && sign == 1) {                return max;            }        }        if (temp * sign > max) {            return max;        }        if (temp * sign < min) {            return min;        }        return (int) temp * sign;    }}

 

leetcode string to integer