精选力扣500题 第26题 LeetCode 415. 字符串相加【c++ / java 详细题解】

1、题目

给定两个字符串形式的非负整数num1num2,计算它们的和。

提示:

  • num1num2 的长度都小于 5100
  • num1num2 都只包含数字0-9
  • num1num2 都不包含任何前导零
  • 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式

2、思路

(高精度模板题)

  • 1、若t1表示第一个数当前位数的大小,t2表示第二个数当前位数的大小,next表示进位数

  • 2、从个位数开始进行相加,使用t记录(t1 + t2 + next)得出的结果,t % 10为该位数确定好的元素,进行下一个位数操作时,需要t /= 10

3、c++版代码

class Solution {
public:
    vector<int> add(vector<int>& A, vector<int>& B) {
        vector<int> C;
        for (int i = 0, t = 0; i < A.size() || i < B.size() || t; i ++ ) {
            if (i < A.size()) t += A[i];
            if (i < B.size()) t += B[i];
            C.push_back(t % 10);
            t /= 10;
        }
        return C;
    }

    string addStrings(string a, string b) {
        vector<int> A, B;
        for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
        for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');
        auto C = add(A, B);
        string c;
        for (int i = C.size() - 1; i >= 0; i -- ) c += to_string(C[i]);
        return c;
    }
};

4、java版代码

class Solution {
    public String addStrings(String num1, String num2) {
        int n = num1.length();
        int m = num2.length();
        int i = n - 1 , j = m - 1 ;
        int c = 0 ;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 || j >= 0 ) {
            int a = i >= 0 ? num1.charAt(i) - '0' : 0 ;
            int b = j >= 0 ? num2.charAt(j) - '0' : 0 ;
            int sum = a + b + c ;
            sb.append(sum % 10) ;
            c = sum / 10 ;
            i--;
            j--;
        }
        if (c > 0) {
            sb.append(c);
        }
        return sb.reverse().toString();

    }
}

5、高精度java模板整理

1、高精度加法

import java.io.*;
import java.math.BigInteger;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String[] s = cin.readLine().split(" ");
        BigInteger n = new BigInteger(s[0]);
        s = cin.readLine().split(" ");
        BigInteger m = new BigInteger(s[0]);
        System.out.println(m.add(n));
    }
}

2、高精度减法

import java.io.*;
import java.math.BigInteger;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String[] s = cin.readLine().split(" ");
        BigInteger n = new BigInteger(s[0]);
        s = cin.readLine().split(" ");
        BigInteger m = new BigInteger(s[0]);
        System.out.println(n.subtract(m));
    }
}

3、高精度乘法

import java.io.*;
import java.math.BigInteger;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String[] s = cin.readLine().split(" ");
        BigInteger n = new BigInteger(s[0]);
        s = cin.readLine().split(" ");
        BigInteger m = new BigInteger(s[0]);
        System.out.println(n.multiply(m));
    }
}

4、高精度除法

import java.io.*;
import java.math.BigInteger;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String[] s = cin.readLine().split(" ");
        BigInteger n = new BigInteger(s[0]);
        s = cin.readLine().split(" ");
        BigInteger m = new BigInteger(s[0]);
        System.out.println(n.divide(m));
        System.out.println(n.remainder(m));
    }
}

原题链接:415. 字符串相加
在这里插入图片描述

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林小鹿@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值