Sword finger offer-67-convert string to integer

Keywords: Java Junit

subject
Please write a function StrToInt to convert string to integer. Of course, you can't use atoi or other similar library functions.

Back to the top
thinking
The topic is very simple, mainly to realize the conversion of each character into a number, and the accumulation can be done. However, there are many special situations that need to be considered, such as null, empty string, with sign, character is not a number, overflow, etc.

For illegal special input, the return value is 0, and a global variable is used to mark it.

When writing code, be sure to consider all kinds of test cases.

package offer;

import org.junit.Test;

/**
 * Convert a string to an integer (implement the function of Integer.valueOf(string), but return 0 if the string does not meet the number requirements).
 * Library functions that require integers that cannot be converted using strings. If the value is 0 or the string is not a legal value, 0 will be returned.
 * @author kankan
 * @creater 2019-07-24 20:16
 */
public class Solution67 {

    public  int StrToInt(String str) {
        //Judge whether the input is legal
        if (null == str || str.trim().equals("")){
            return 0;
        }
        // symbol=0, indicating that the number is positive; symbol=1, indicating that the number is negative; start is used to distinguish whether the first bit is a sign bit.
        int symbol = 0;
        int start = 0;
        char[] chars = str.trim().toCharArray();
        if ('+' == chars[0]){
            start = 1;
        }else if ('-' == chars[0]){
            start = 1;
            symbol = 1;
        }
        int result = 0;
        for (int i = start; i < chars.length; i++) {
            if (chars[i] > '9' || chars[i] < '0'){
                return 0;
            }
            int sum = result * 10 + (int) (chars[i] - '0');
            if ((sum - (int)(chars[i] - '0')) / 10 != result){
                return 0;
            }
            result=result * 10 + (int) (chars[i] - '0');
            /*
             * I think it's wrong to judge whether overflow is the most popular one in java, for example
             * When the input is value=2147483648, the representation inside the computer should be - 2147483648
             * Obviously, value > integer.max'value does not hold.
            */
        }
        // Note: the n-th power of - 1 in java cannot be used: (- 1)^n. '^' exclusive or operation
        // Note that when value=-2147483648, value=-value
        result = (int) Math.pow(-1, symbol) * result;
        return result;
    }

    // 1. Function test (positive, negative, zero, number with sign)
    @Test
    public void testA(){
        System.out.println(new Solution67().StrToInt("1948243")==1948243);

        System.out.println(new Solution67().StrToInt("+1948243")==1948243);

        System.out.println(new Solution67().StrToInt("-1948243")==-1948243);

        System.out.println(new Solution67().StrToInt("-0")==0);

    }
    // 2. Boundary value test (maximum positive integer, minimum negative integer)
    @Test
    public void testB(){
        String str = "+2147483647";
        int strToInt = new Solution67().StrToInt(str);
        System.out.println(strToInt);

        String str1 = "-2147483648";
        int strToInt1 = new Solution67().StrToInt(str1);
        System.out.println(strToInt1);
    }
    // 3. Special test (null, null string, only sign, illegal character)
    @Test
    public void testC(){
        System.out.println(new Solution67().StrToInt(""));

        System.out.println(new Solution67().StrToInt(null));

        System.out.println(new Solution67().StrToInt("1a33"));

        System.out.println(new Solution67().StrToInt("+++"));
    }
}

Posted by php_guy on Wed, 16 Oct 2019 12:39:38 -0700