`

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.

计算两个大数的乘法,如果直接计算,就会溢出。假设两个数的长度分别为m, n。那么它们相乘之后的长度肯定为m+n(有进位), 或m + n - 1(没有进位)。我们建立一个长度为m + n 的数组,用来求解每一位上对应的值,维护一个进位。代码如下:
public class Solution {
    public String multiply(String num1, String num2) {
        if(num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0) return "";
        int[] digit = new int[num1.length() + num2.length()];
        for (int i = num1.length() - 1; i >= 0; i--) {
            int a = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
                int b = num2.charAt(j) - '0';
                digit[i + j + 1] += a * b;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = digit.length - 1; i > 0; i--) {
            int num = digit[i] % 10;
            int carry = digit[i] / 10;
            sb.append(num);
            digit[i - 1] += carry;
        }
        //检查最高位是否有进位,如果有进位,将进位加入字符串中
        if(digit[0] > 0) sb.append(digit[0]);
        /*
            检查最后一位是否为0,如果为0说明字符串一定为一个
            全0的序列。因为在上一步我们已经判断是否有进位,如
            果为0说明没有进位,只有全为0的情况下,字符串倒数第
            二位才可能为0
        */
        if(sb.charAt(sb.length() - 1) == '0') return "0";
        
        return sb.reverse().toString();
    }
}
分享到:
评论

相关推荐

    cpp-算法精粹

    Multiply Strings Substring with Concatenation of All Words Pascal's Triangle Pascal's Triangle II Spiral Matrix Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on ...

    LeetCode最全代码

    ...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...

    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的各式演算法

    leetcode卡-LeetCode:LeetCode题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: ...Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi

    week1-wednesday

    <<<<<<< HEAD#周三9/16的概述 回顾昨天,去做功课 介绍数组 在课堂练习中(成对) ...//1....//2....//3.... Add, subtract, multiply and divide them. ... Add, subtract, multiply and divide the strings

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Multiply two numbers without using arithmetic operators Chapter 13. Compute the two’s complement for a given integer Chapter 14. Isolate the rightmost bit set to 1 Chapter 15. Create a mask for ...

    AT&T Assembly Language

    Multiply by shifting 224 Dividing by shifting 225 Rotating bits 226 Decimal Arithmetic 227 Unpacked BCD arithmetic 227 Packed BCD arithmetic 229 Logical Operations 231 Boolean logic 231 Bit testing ...

    EurekaLog_7.5.0.0_Enterprise

    5)....Added: Improvements for call stack of dynarrays/strings allocations (leaks) 6)....Added: "Elem size" when reporting leaks in dynarrays 7)....Added: Streaming unpacked debug info into temporal ...

    ARM® Compiler v5.06 for µVision® armasm User Guide

    8.3 Fused Multiply-Add extension for VFP 8.4 Extension register bank mapping in VFP 8.5 VFP views of the extension register bank 8.6 Load values to VFP registers 8.7 Conditional execution of VFP ...

    一个win32下的ARM开源编译器

    FASMARM v1.42 This package is an ARM assembler add-on for FASM. FASMARM currently supports the full range of instructions for 32-bit and 64-bit ARM processors and coprocessors up to and including v8...

    深入理解计算机系统(英文版)

    2.1.5 Representing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.6 Representing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.7...

Global site tag (gtag.js) - Google Analytics