leetcode415

改进前的代码:
public class leetcode415 {
    private StringBuilder res = new StringBuilder();
    public String addStrings(String num1, String num2) {
        int length1 = num1.length();
        int length2 = num2.length();
        int count = 1;
        boolean isAddOne = false;
        while (length1 - count >=0 && length2 - count >= 0) {
            int temp = num1.charAt(length1-count) - '0' + num2.charAt(length2-count)-'0';
            if (isAddOne) {
                temp++;
            }
            if (temp < 10) {
                res.append(temp);
                isAddOne = false;
            } else {
                res.append(temp%10);
                isAddOne = true;
            }
            count++;
        }
        if (length1 == length2 && isAddOne) {
            res.append(1);
        } else if (length1 > length2) {
            processMul(num1,length1,count,isAddOne);
        } else {
            processMul(num2,length2,count,isAddOne);
        }

        return res.reverse().toString();
    }

    private void processMul(String s, int length,int count,boolean isAddOne) {
        while (length - count >=0) {
            int temp = s.charAt(length-count) - '0';
            if (isAddOne) {
                temp++;
            }
            if (temp < 10) {
                res.append(temp);
                isAddOne = false;
            } else {
                res.append(temp%10);
                isAddOne = true;
            }
            count++;
        }
        if (isAddOne) {
            res.append(1);
        }
    }
}

改进点:
1.较短的那个字符串可以在前面补0,不用重新执行那个循环!

第一次改进后:
public class leetcode415 {
    public String addStrings(String num1, String num2) {
        int length1 = num1.length();
        int length2 = num2.length();
        int count = 1;
        StringBuilder res = new StringBuilder();
        boolean isAddOne = false;
        while (length1 - count >=0 || length2 - count >= 0) {
            int number1 = length1 - count >= 0?num1.charAt(length1 - count)-'0':0;
            int number2 = length2 - count >= 0?num2.charAt(length2 - count)-'0':0;
            int temp = number1 + number2;
            if (isAddOne) {
                temp++;
            }
            if (temp < 10) {
                res.append(temp);
                isAddOne = false;
            } else {
                res.append(temp%10);
                isAddOne = true;
            }
            count++;
        }
        if (isAddOne) {
            res.append(1);
        }

        return res.reverse().toString();
    }
}

改进点:
1.length1 - count 和 length2 - count 没必要,可以直接用两个变量来代替。
2.不用区分temp是否小于10,res.append(temp%10)都成立。
3.isAddOne 的赋值可以用三元运算符。

第二次改进后:
public class leetcode415 {
    public String addStrings(String num1, String num2) {
        int p1 = num1.length() - 1;
        int p2 = num2.length() - 1;
        StringBuilder res = new StringBuilder();
        boolean isAddOne = false;
        while (p1 >=0 || p2 >= 0) {
            int number1 = p1 >= 0?num1.charAt(p1)-'0':0;
            int number2 = p2 >= 0?num2.charAt(p2)-'0':0;
            int temp = number1 + number2;
            if (isAddOne) {
                temp++;
            }
            res.append(temp%10);
            isAddOne = temp>=10;
            p1--;
            p2--;
        }
        if (isAddOne) {
            res.append(1);
        }

        return res.reverse().toString();
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值