leetcode 43 Multiply Strings 大数相乘

感觉是大数相乘算法里面最能够描述、模拟演算过程的思路

class Solution {
    public String multiply(String num1, String num2) {
        if(num1.charAt(0) == ‘0‘ || num2.charAt(0) == ‘0‘){
            return "0";
        }

        int len1 = num1.length();
        int len2 = num2.length();
        int len = len1+len2;
        int[] arr = new int[len];
        for(int i=len1-1;i>=0;i--){
            for(int j=len2-1;j>=0;j--){
                arr[i+j+1] += (num1.charAt(i)-‘0‘)*(num2.charAt(j)-‘0‘);
            }
        }
        for(int i=len-1;i>0;i--){
            if(arr[i]>=10){
                arr[i-1] += arr[i]/10;
                arr[i]%=10;
            }
        }
        int i=0;
        if(arr[i] == 0) i++;
        String res = "";
        while(i<len){
            res = res+arr[i++];
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/mxk-star/p/8627657.html

时间: 2024-08-27 10:04:13

leetcode 43 Multiply Strings 大数相乘的相关文章

Java [Leetcode 43]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(St

43. Multiply Strings 字符串相乘

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading z

[LeetCode#43]Multiply Strings

Problem: 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. Analysis: The naive solution of this problem is to following the routine of multipli

Multiply Strings 大数相乘 java

先贴上代码 1 public String multiply(String num1, String num2) { 2 String str = ""; 3 StringBuffer sb = new StringBuffer(num1); 4 num1 = sb.reverse().toString(); 5 sb = new StringBuffer(num2); 6 num2 = sb.reverse().toString(); 7 int[] res = new int[nu

19.2.4 [LeetCode 43] Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3" Output: "6" Example 2: Input: num1 = "123&

&lt;LeetCode OJ&gt; 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

43. Multiply Strings(js)

43. Multiply Strings Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3" Output: "6" Example 2: Inp

【一天一道LeetCode】#43. Multiply Strings

一天一道LeetCode系列 (一)题目 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. Converting the input string to integer is NOT allowed. You should NOT us

【LeetCode】Multiply Strings

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. 我借鉴了JackBauer的一些思想,将乘积逆序存放在int数组result中. 记num1当前为第ind1位(个位为0),num2当前为ind2位,则