[LeetCode] 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.

这道题让我们求两个字符串的相加,之前LeetCode出过几道类似的题目,比如二进制数相加,还有链表相加,或是字符串加1,基本思路很类似,都是一位一位相加,然后算和算进位,最后根据进位情况看需不需要补一个高位,难度不大,参见代码如下:

class Solution {
public:
    string addStrings(string num1, string num2) {
        string res = "";
        int m = num1.size(), n = num2.size(), i = m - 1, j = n - 1, carry = 0;
        while (i >= 0 || j >= 0) {
            int a = i >= 0 ? num1[i--] - ‘0‘ : 0;
            int b = j >= 0 ? num2[j--] - ‘0‘ : 0;
            int sum = a + b + carry;
            res.insert(res.begin(), sum % 10 + ‘0‘);
            carry = sum / 10;
        }
        return carry ? "1" + res : res;
    }
};

类似题目:

Add Digits

Add Binary

Add Two Numbers

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-09-30 14:16:17

[LeetCode] Add Strings 字符串相加的相关文章

[LeetCode] 415. Add Strings 字符串相加

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

[LeetCode] Multiply Strings 字符串相乘

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 这道题让我们求两个字符串数字的相乘,输入的两个数和返回的数都是以字符串格式储存的,这样做的原因可能是这样可以计算超大数相乘,可以不受int或long的数值范围的约束,那么我们该如何来计算

Leetcode:Add Binary 二进制相加

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路同十进制的大数相加.代码如下: class Solution { public: string addBinary(string a, string b) { size_t len_a = a.length(); size_

Leetcode: Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

36. leetcode 415. Add Strings

415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain

LeetCode:字符串相加【415】

LeetCode:字符串相加[415] 题目描述 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100.num1 和num2 都只包含数字 0-9.num1 和num2 都不包含任何前导零.你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式. 题目分析 这道题其实很简单,我们要搞清楚手工计算两数之和的流程.两数相加,和如果大于10的话就有进位,进位最高为1,默认为0,该位相加的和应为sum%

LeetCode——Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 求数字字符串的二进制和.同之前的数组代表数字,两个数组相加一样,只不过进位变成了2.可能两个串的长度不一样,故逆转,从左到右加下去,最后再逆转. public static String addBinary(String a,

LeetCode: Multiply Strings [042]

[题目] Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. [题意] 给定用字符串表示的整数,返回两个数的乘积结果字符串.两个数字都非负,且能任意大. [思路] 1. 考虑其中一个数是0的情况 2. 模拟乘法运算过程 维护一个vecto

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa