`

Leetcode - Multiply String

 
阅读更多
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.

[分析] Naive的做法是模拟笔算乘法操作,从高位到低位的顺序将num2 上的每一位同num1 相乘,由于运算顺序从高到低,每次运算时将上一次运算结果后加0,(相当于*10)后同本次乘积结果加和。 高效做法(Method 2)思路:num1 * num2, num2上的每一位依次和num1中的每一位相乘,num2的个位同num1的个位相乘结果包含在最终乘积的个位上,同理num2的个位同num1的十位相乘结果包含在最终乘积的十位上,num2的十位同num1的个位相乘结果也包含在最终乘积的十位上,一般的,num2的第 i 位 同num1的第 j 位相乘结果包含在最终乘积的第 i + j 位,遍历两个数的每一位,可以得到最终乘积各位上的结果。这样得到的乘积各位上可能是两位数,因此还需要处理使得乘积结果各位上为单个数字,处理方式就是依次进位。Method 1有O(m * n)次 *、\、% 以及字符串append操作, O(m * (m + n))次 +, append操作,而Method 2仅有O(m * n)次 *、+ 以及O(m + n)次 \、%、append操作,我们知道除法和取模是最耗时的,所以前者超时也就不难怪了。

    // method 2: 
    public String multiply(String num1, String num2) {
        if (num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0)
            return null;
        if (num1.length() < num2.length())
            return multiply(num2, num1);
        if (num2.equals("0")) return "0";
        if (num2.equals("1")) return num1;
        int[] result = new int[num1.length() + num2.length() - 1];
        for (int i = num2.length() - 1; i >= 0; i--) {
            int digit1 = num2.charAt(i) - '0';
            for (int j = num1.length() - 1; j >= 0; j--) {
                result[i + j] += digit1 * (num1.charAt(j) - '0');
            }
        }
        int carry = 0;
        for (int i = result.length - 1; i >= 0; i--) {
            result[i] += carry;
            carry = result[i] / 10;
            result[i] %= 10;
        }
        StringBuilder val = new StringBuilder();
        if (carry > 0)
            val.append(carry);
        for (int i = 0; i < result.length; i++)
            val.append(result[i]);
        return val.toString();
    }
    // method 1 : 
    public String multiply1(String num1, String num2) {
        if (num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0)
            return null;
        String result = "";
        for (int i = 0; i < num2.length(); i++) {
            result = add(result + '0', multiOneDigit(num1, num2.charAt(i) - '0'));
        }
        return result;
    }
    private String add(String num1, String num2) {
        if (num1.length() < num2.length()) {
            String tmp = num2;
            num2 = num1;
            num1 = tmp;
        }
        StringBuilder sum = new StringBuilder();
        int carry = 0, val = 0;
        int i = num1.length() - 1, j = num2.length() - 1;
        while (j >= 0) {
            val = (num1.charAt(i) - '0') + (num2.charAt(j) - '0');
            sum.append(val - 10); // val % 10
            carry = val >= 10 ? 1 : 0; // val / 10
            i--; j--;
        }
        while (i >= 0) {
            val = (num1.charAt(i--) - '0') + carry;
            sum.append(val - 10);
            carry = val >= 10 ? 1 : 0;
        }
        if (carry > 0) sum.append(carry);
        return sum.reverse().toString();
    }
    private String multiOneDigit(String num1, int num2) {
        StringBuilder result = new StringBuilder();
        int carry = 0, val = 0;
        for (int i = num1.length() - 1; i >= 0; i--) {
            val = (num1.charAt(i) - '0') * num2 + carry;
            result.append(val % 10);
            carry = val / 10;
        }
        if (carry > 0)
            result.append(carry);
        return result.reverse().toString();
    }
分享到:
评论

相关推荐

    _leetcode-python.pdf

    - String to Integer (atoi): 将字符串转换为整数,需要处理各种边界情况,例如溢出、非法输入等。 - Reverse Integer: 将一个整数反转。在处理大数时需要注意溢出问题。 - Regular Expression Matching: 给定一个...

    LeetCode最全代码

    * [String](https://github.com/kamyu104/LeetCode#string) * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue]...

    Leetcode扑克-leetcode:Leetcode算法实践

    Leetcode扑克 Leetcode Starting from 2020/08/02 Leetcode practice with Python (for now) Daily Challenge + Selected Questions From Using Leetcode Plugin for Solutions ...String ...Multiply S

    leetcode卡-LeetCode:LeetCode题解

    String to Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- strcpy :star: :star: :star: 字符串复制,返回值,赋值语句 0028 strStr :star: :...

    C语言入门-leetcode练习之第43题字符串相乘.zip

    #include &lt;string.h&gt; char* multiply(char* num1, char* num2) { int len1 = strlen(num1), len2 = strlen(num2); if (len1 == 0 || len2 == 0) return "0"; // 初始化二维数组存储中间结果 int arr[len1 + ...

    leetcode字符串括号level-leetcode:LeetCode解题记录

    multiply 字符串相乘 reverseWords 翻转字符串里的单词 simplifyPath 简化路径 restoreIPAddresses 复原IP地址 threeSum 三数之和 search 搜索旋转排序数组 1. 3. 9. 75. 209. 219. 167. 268. 344. 349. 454. 447. ...

    leetcodepython001-LeetCode:力码

    Multiply Strings 066 Add Binary Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to be a softwair engineer: 其他人详解 Python的各式演算法

    c++-c++编程基础之leetcode题解第43题字符串相乘.zip

    string multiply(string num1, string num2) { if (num1 == "0" || num2 == "0") return "0"; int len1 = num1.length(), len2 = num2.length(); vector&lt;int&gt; res(len1 + len2, 0); for (int i = len1 - 1; ...

Global site tag (gtag.js) - Google Analytics