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.

题意给两个字符串表示的数字,计算他们的乘积。

其实就是手写一个大数乘法,先翻转字符串便于从低位开始计算。

模拟乘法的运算过程,把中间结果存在data中,最后在考虑data的进位并存到结果字符串里。

注意点的就是考虑结果的前置0不要添加进去。


int data[100000];
class Solution {
public:
    string multiply(string num1, string num2) {
        reverse(num1.begin(),num1.end());
        reverse(num2.begin(),num2.end());
        memset(data,0,sizeof(data));
        int len1=num1.length();
        int len2=num2.length();
        int i,j;
        for(i=0;i=0;--i)
        {
            if(flag==0&&data[i]==0)continue;
            else
            {
                flag=1;
                result+=(char)(data[i]+'0');
            }
        }
        if(flag==0)return "0";
        else return result;
    }
};


你可能感兴趣的:(leetcode面试算法题,leetcode题解,面试算法)