【剑指offer】面试题67:把字符串转换成整数

题目

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。
数值为0或者字符串不是一个合法的数值则返回0

思路

其实思路很简单,但是要考虑的非法输入和边界值条件很多
合法的输入:1.数字 2.符号(+-)+ 数字

非法输入:
1.null
2.字符串为空
3.字符串只有一个符号(+-)
4.字符串中带有非数字字符
5.字符串超过int能表示的范围

代码

public class _67_StringToInt {

    public boolean isValid = false;

    public int StrToInt(String str) {
        if(str == null || str.trim().equals("")) {
            return 0;
        }
        int index = 0;
        long result = 0;
        boolean isPositive = true;
        if(str.charAt(0) == '+' || str.charAt(0) == '-') {
            ++index;
            // 字符串中只有正负号
            if(index == str.length())
                return 0;
            if(str.charAt(0) == '-')
                isPositive = false;
        }

        for(int i = index; i < str.length(); ++i) {
            if(str.charAt(i) < '0' || str.charAt(i) > '9') {
                return 0;
            }
            result *= 10;
            result += (str.charAt(i) - '0') * (isPositive ? 1 : -1);
            if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE)
                return 0;
        }
        isValid = true;
        return (int) result;
    }
}

测试

public class _67_Test {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    /**
     * 功能测试
     * 1.带符号(+-)
     * 2.不带符号
     */
    private static void test1() {
        _67_StringToInt sti = new _67_StringToInt();
        int result = sti.StrToInt("666");
        MyTest.equal(result, 666);
        MyTest.equal(true, sti.isValid);

        result = sti.StrToInt("+666");
        MyTest.equal(result, 666);
        MyTest.equal(true, sti.isValid);

        result = sti.StrToInt("-666");
        MyTest.equal(result, -666);
        MyTest.equal(true, sti.isValid);
    }

    /**
     * 边界测试
     * 1.0
     * 2.Integer.MAX_VALUE
     * 3.Integer.MIN_VALUE
     */
    private static void test2() {
        _67_StringToInt sti = new _67_StringToInt();
        int result = sti.StrToInt("0");
        MyTest.equal(result, 0);
        MyTest.equal(true, sti.isValid);

        result = sti.StrToInt(String.valueOf(Integer.MAX_VALUE));
        MyTest.equal(result, Integer.MAX_VALUE);
        MyTest.equal(true, sti.isValid);

        result = sti.StrToInt(String.valueOf(Integer.MIN_VALUE));
        MyTest.equal(result, Integer.MIN_VALUE);
        MyTest.equal(true, sti.isValid);
    }

    /**
     * 极端测试
     * 1.null
     * 2.空字符串
     * 3.超过int表示范围
     * 4.只带符号
     * 5.非法表示
     */
    private static void test3() {
        _67_StringToInt sti = new _67_StringToInt();
        int result = sti.StrToInt(null);
        MyTest.equal(result, 0);
        MyTest.equal(false, sti.isValid);

        result = sti.StrToInt("");
        MyTest.equal(result, 0);
        MyTest.equal(false, sti.isValid);

        result = sti.StrToInt("1436468468422");
        MyTest.equal(result, 0);
        MyTest.equal(false, sti.isValid);

        result = sti.StrToInt("+");
        MyTest.equal(result, 0);
        MyTest.equal(false, sti.isValid);

        result = sti.StrToInt("-");
        MyTest.equal(result, 0);
        MyTest.equal(false, sti.isValid);

        result = sti.StrToInt("123a86");
        MyTest.equal(result, 0);
        MyTest.equal(false, sti.isValid);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值