【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.

【解析】

题意:两个字符串表示的非负整数相乘,用字符串的形式返回积。

思路:逐位相乘。

关键:中间结果如何保存?如果用字符串保存中间结果,频繁该值不太方便,所以还是用整数数组保存,最后再转为字符串比较方便。

public class Solution {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) return "0";

        int[] table = new int[num1.length() + num2.length()];//存放乘积,低位存低位
        int len = 0; //积的长度,用于转为字符串

        for (int i = num1.length() - 1; i >= 0; i--) {
            int digit1 = (int)(num1.charAt(i) - '0');
            int carry = 0;
            int k = num1.length() - 1 - i;  //本轮乘积结果开始的位置

            for (int j = num2.length() - 1; j >= 0; j--, k++) {
                int digit2 = (int)(num2.charAt(j) - '0');
                int res = digit1 * digit2;  //位与位的乘积
                res += table[k];            //加上上一轮乘积第k位的数
                res += carry;               //加上进位到第k位的数
                table[k] = res % 10;        //进位后剩余的数
                carry = res / 10;           //进位
            }

            //更新结果的长度
            if (k - 1 > len) {
                len = k - 1;
            }

            //如果最后进位不为0
            if (carry > 0) {
                table[k] += carry;
                if (table[k] > 9) {
                    table[k + 1] = table[k] / 10;
                    table[k] %= 10;
                    k++;
                }
                if (k > len) {  //更新结果的长度
                    len = k;
                }
            }
        }

        //把积转为字符串
        String ans = "";
        for (int i = len; i >= 0; i--) {
            ans += String.valueOf(table[i]);
        }

        return ans;
    }
}
时间: 2024-11-08 03:34:24

【LeetCode】Multiply Strings 解题报告的相关文章

LeetCode: Multiply Strings 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

[leetcode]Isomorphic Strings 解题报告 C语言

[题目] Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charact

[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. 解题思路:两个非负数字字符串的相乘.其实就是大数乘法.算法的关键是

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: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

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——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. 原题链接:https://oj.leetcode.com/problems/multiply-strings/ 按照乘法原理,从个位开始一位一位相乘相加. public class Mu