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;
    }
}
时间: 2024-12-21 12:43:36

leetcode string to integer的相关文章

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode——String to Integer (atoi)

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 spe

[leetcode] [leetcode] String to Integer (atoi)

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 spe

[LeetCode] String to Integer (atoi) [7]

题目 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

leetcode - String to Integer (atoi) 字符串转整数

Implement atoi to convert a string to an integer. 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. 题目:实现字符串转整数 注意事项:考虑好各种可能的输入(坑); public class Solution

[LeetCode] String to Integer (atoi)

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 spe

[LeetCode] String to Integer (atoi) 字符串

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 spe

[LeetCode] String to Integer (atoi) 字符串转为整数

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 spe

[Leetcode] String to integer atoi 字符串转换成整数

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 spe