leetcode oj java Add Strings

一、问题描述:

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

二、解决思路:

       不能转化为integer,就用char数组,因为char之间直接相加返回的是ascii码。  两个char相加再减去48*2  就得到十进制的结果。(0-9的ascii是48-57)

       剩下的就是实现基本的十进制加法啦。要考虑的因素比较多~ 一定要细心


三、代码:

public class Solution {
    public String addStrings(String num1, String num2) {
        String res = "";
        char[] c1 = num1.toCharArray();
        char[] c2 = num2.toCharArray();
        int temp = 0;
        int re = 0;

        for (int i = c1.length - 1, j = c2.length - 1; i > -1 && j > -1; i--, j--) {
            char cn1 = c1[i];
            char cn2 = c2[j];
            re = cn1 + cn2 + temp - 48 * 2;
            if (re > 9) {
                temp = re / 10;
                re = re % 10;
            } else {
                temp = 0;
            }
            res = re + res;;
        }

        if (c1.length == c2.length && temp > 0) {
            res = temp + res;
            return res;
        }

        if (c1.length - c2.length > 1) {
            re = c1[c1.length - c2.length - 1] + temp - 48;
            if (re > 9) {
                temp = re / 10;
                re = re % 10;
            } else {
                temp = 0;
            }
            res = re + res;
            for (int i = c1.length - c2.length - 2; i > -1; i--) {
                re = c1[i] + temp - 48;
                if (re > 9) {
                    temp = re / 10;
                    re = re % 10;
                } else {
                    temp = 0;
                }
                res = re + res;
            }
            if (temp > 0) {
                res = temp + res;
            }
        }

        if (c1.length - c2.length == 1) {
            re = c1[c1.length - c2.length - 1] + temp - 48;
            res = re + res;
        }

        if (c2.length - c1.length > 1) {
            re = c2[c2.length - c1.length - 1] + temp - 48;
            if (re > 9) {
                temp = re / 10;
                re = re % 10;
            } else {
                temp = 0;
            }
            res = re + res;
            for (int i = c2.length - c1.length - 2; i > -1; i--) {
                re = c2[i] + temp - 48;
                if (re > 9) {
                    temp = re / 10;
                    re = re % 10;
                } else {
                    temp = 0;
                }
                res = re + res;
            }
            if (temp > 0) {
                res = temp + res;
            }
        }

        if (c2.length - c1.length == 1) {
            re = c2[c2.length - c1.length - 1] + temp - 48;
            res = re + res;
        }

        return res;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值