Multiply Strings 字符串相乘

http://www.cnblogs.com/TenosDoIt/p/3735309.html

https://blog.csdn.net/fly_yr/article/details/48055617

class Solution {
public:
    string multiply(string num1, string num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        vector<int> res(len1+len2,0);
        int k = len1 + len2 - 2;          第一轮乘出来的结果只有len1+len2-1个,数组的index为0再减1
        for(int i = 0;i < len1;i++){
            for(int j = 0;j < len2;j++){
                res[k - i - j] += (num1[i] - ‘0‘) * (num2[j] - ‘0‘);   num1[0]是原本数的最高位,这里做了个转换,把最低位的数放在了0位置
            }
        }
        int carry = 0;
        for(int i = 0;i < len1 + len2;i++){
            res[i] += carry;
            carry = res[i]/10;
            res[i] = res[i]%10;
        }
        int i = k + 1;
        while(res[i] == 0)          找寻第一个非0的位置
            i--;
        if(i < 0)
            return "0";
        string result;
        for(;i >= 0;i--)
            result.push_back(res[i] + ‘0‘);
        return result;
    }
};

原文地址:https://www.cnblogs.com/ymjyqsx/p/9464311.html

时间: 2024-10-07 14:31:31

Multiply Strings 字符串相乘的相关文章

[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. 这道题让我们求两个字符串数字的相乘,输入的两个数和返回的数都是以字符串格式储存的,这样做的原因可能是这样可以计算超大数相乘,可以不受int或long的数值范围的约束,那么我们该如何来计算

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 OJ: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. 给出两个字符串,返回对应数字想乘后的字符串,由于这个字符串可能很大,所以不能采用一般的乘法,这里用的方法是模拟手工的乘法运算,算法 本身很简单,就是当时写的时候有些很小的细节搞错了,找了

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. 要求:字符串表示的数字可能无穷大,并且非负. class Solution { private: vector<string> tempStrs; public: string add

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

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. string multiply(string& num, char ch){ int n = ch - '0'; string s; int carry = 0; int x; for(

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 =

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&q

Multiply Strings

package cn.edu.xidian.sselab;/** * title:Multiply Strings * content: * 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. * 读已知条件可以,两个String转换