LeetCode 43. 字符串相乘(Multiply Strings)

43. 字符串相乘
43. Multiply Strings

题目描述
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

LeetCode43. Multiply Strings中等

示例 1:

输入: num1 = "2", num2 = "3"
输出: "6"

示例?2:

输入: num1 = "123", num2 = "456"
输出: "56088"

说明:

  1. num1?和?num2?的长度小于110。
  2. num1 和?num2 只包含数字?0-9。
  3. num1 和?num2?均不以零开头,除非是数字 0 本身。
  4. 不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。

Java 实现

class Solution {
    public String multiply(String num1, String num2) {
        int m = num1.length(), n = num2.length();
        int[] pos = new int[m + n];
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                int p1 = i + j, p2 = i + j + 1;
                int sum = (num1.charAt(i) - '0') * (num2.charAt(j) - '0') + pos[p2];
                pos[p1] += sum / 10;
                pos[p2] = sum % 10;
            }
        }
        StringBuffer sb = new StringBuffer();
        for (int p : pos) {
            if (!(sb.length() == 0 && p == 0)) {
                sb.append(p);
            }
        }
        return sb.length() == 0 ? "0" : sb.toString();
    }
}

相似题目

  • 2. 两数相加 Add Two Numbers
  • 66. 加一 Plus One
  • 67. 二进制求和 Add Binary
  • 415. 字符串相加 Add Strings

参考资料

原文地址:https://www.cnblogs.com/hglibin/p/10989357.html

时间: 2024-10-07 23:58:25

LeetCode 43. 字符串相乘(Multiply Strings)的相关文章

LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法

题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例 2: 输入: num1 = "123", num2 = "456" 输出: "56088" 说明: num1 和 num2 的长度小于110. num1 和 nu

leetcode 第42题 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. 就是实现大数乘法.嘿嘿,我先用long long直接乘试试手感,试过了不行.所以还是要用字符串表示才行. 我是这样做的,先实现两个子函数,一个是实现一个数和一个字符串的数相乘的结果

43. 字符串相乘

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例 2: 输入: num1 = "123", num2 = "456" 输出: "56088" 说明: num1 和 num2 的长度小于110. num1 和 num2 只包

leetcode 43 大数相乘

这题如果如果开挂的话, 可以直接用BigInteger类. 思路 用了三个辅助方法: 1. 大数相加.(顺带实现的,毕竟乘法的过程中需要用到加法) 2. 大数 * 一位数字 (很基础的步骤,列竖式的时候用到) 3. 去除结果的前导0.(很容易忽略,如0 * 123 结果会是 000,正确的结果应该是0) 然后就是利用这三个辅助方法得到最终结果.写法虽然直接但是有点繁琐,后续会考虑优化一下. 1 class Solution { 2 public String multiply(String nu

LeetCodr 43 字符串相乘

思路 用一个数组记录乘积的结果,最后处理进位. 代码 class Solution { public: string multiply(string num1, string num2) { if(num1 == "0" || num2 == "0") return "0"; reverse(num1.begin(), num1.end()); reverse(num2.begin(), num2.end()); int a[num1.lengt

leetcode Multiply Strings

题目连接 https://leetcode.com/problems/multiply-strings/ Multiply Strings Description 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. 普通写法: class

<LeetCode OJ> 43. Multiply Strings

43. Multiply Strings My Submissions Question Total Accepted: 51859 Total Submissions: 231017 Difficulty: Medium 以字符串的形式给定两个数字,返回相乘的结果,注意:结果也是字符串,因为数字可能很大 Given two numbers represented as strings, return multiplication of the numbers as a string. Note

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]Multiply Strings @ Python

原题地址:https://oj.leetcode.com/problems/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. 解题思路:两个非负数字字符串的相乘.其实就是大数乘法.算法的关键是