Java algorithms practice - string conversion integer (atoi)

Topic Link

Title Description

Atoi you to implement a function, it can convert a string to an integer.

First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.

When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.

In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.

Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.

In any case, if the function can not effectively convert, 0 is returned.

Description:

We assume that the environment can store 32-bit signed integer size, then its value range is $ [- 2 ^ {31}, {31} ^ 2 --1] $. If the value exceeds this range, qing return INT_MAX ($ 2 ^ {31} - 1 $) or INT_MIN ($ -2 ^ {31} $).

Example 1

输入: "42"
输出: 42
示例 2:

Example 2

输入: "   -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
     我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。

Example 3

输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。

Example 4

输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
     因此无法执行有效的转换。

Example 5

输入: "-91283472332"
输出: -2147483648
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。 
     因此返回 INT_MIN (−231) 。

answer

public int myAtoi(String str) {
    str = str.trim();  // 删除字符串头尾空格
    if (str.length() == 0) return 0;
    int flag = 1;  // 符号位标识
    int rev = 0;  // 数值(无符号)
    int edge = Integer.MAX_VALUE / 10;  // 判断数值是否超过范围的边界线,这样写可以节省时间
    if (str.charAt(0) == '-') {
        flag = -1;
        str = str.substring(1, str.length());  // 跳过符号位,可不写第二参数
    } else if (str.charAt(0) == '+') {
        str = str.substring(1, str.length());  // 跳过符号位,可不写第二参数
    } else if (!(str.charAt(0) >= '0' && str.charAt(0) <= '9')) {  // 如果开始非空字符不为符号或数字,则直接返回 0
        return 0;
    }
    for (char s : str.toCharArray()) {
        if (s >= '0' && s <= '9') {
            int n = s - '0';  // 计算字符代表值
            if (rev >= edge) {  // 超过边界情况较少,故该判断写于外侧
                if (flag == 1) {
                    if (rev > edge || n > 7) return Integer.MAX_VALUE;
                } else {
                    if (rev > edge || n > 8) return Integer.MIN_VALUE;
                }
            }
            rev = rev * 10 + n;
        } else {
            break;
        }
    }
    return rev * flag;
}

Complexity Analysis

  • Time complexity: $ O (n) $.
  • Space complexity: $ O (n) $.

Notes

The problem is not difficult, note range.

Guess you like

Origin www.cnblogs.com/mxwbq/p/10945650.html