一天一道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 use internal library such as BigInteger.

(二)解题

两个string代表的数字相乘,主要就是要求写出大数相乘的代码,具体看注释

/*
本题的思路主要是:num1的第i位和num2的第j位相乘等于乘积的第i+j位
m位数和n位数相乘的结果最大位m+n位!
1.为了便于理解,将两个string首先反转
2.两个for循环,从num1的0位开始,依次乘上num2的0-size位,注意考虑到进位的问题
3.对于结果ret,先反转,如果全为0,则直接返回0,否则去掉前面连续的0,即为最后的结果
*/
class Solution {
public:
    string multiply(string num1, string num2) {
        reverse(num1.begin(),num1.end());//反转num1
        reverse(num2.begin(),num2.end());//反转num2
        string ret(num1.size()+num2.size(),'0');
        for(int i = 0 ; i < num1.size() ; i++)
        {
            int carry = 0;//处理进位
            int j = 0;
            for(; j < num2.size() ; j++)
            {
                carry += (ret[i+j]-'0') + (num1[i]-'0')*(num2[j]-'0');
                ret[i+j] = ('0'+carry%10) ;
                carry /=10;
            }
            if(carry!=0)//处理最后的进位
            {
                ret[i+j] = ('0'+carry);
            }
        }
       //对最后的字符串进行处理
        reverse(ret.begin(),ret.end());
        for(auto iter=ret.begin() ; iter!=ret.end() ; )
        {
            if(*iter == '0'&&ret.size()>1) iter = ret.erase(iter);//如果为‘0’则删除
            else break;//第一位不为‘0’的数就退出
        }
        return ret;
    }
};

【一天一道LeetCode】#43. Multiply Strings的更多相关文章

  1. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  2. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  3. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  4. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  5. [leetcode]43. Multiply Strings高精度乘法

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  6. LeetCode 43 Multiply Strings(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

  7. leetcode 43. Multiply Strings(高精度乘法)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

  9. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  10. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

随机推荐

  1. CF | Alyona and Numbers

    After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down ...

  2. webpack dev server 和 sublime text 配合时需要注意的地方

    参考:https://webpack.js.org/guides/development/ Adjusting Your Text Editor Some text editors have a &q ...

  3. sublime snippet 示例

    <snippet> <content><![CDATA[local ${1:M} = {} function ${1:M}.new(cls, self) self = s ...

  4. NLP系列(1)_从破译外星人文字浅谈自然语言处理基础

    作者:龙心尘 &&寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50543337 ht ...

  5. PGM:部分观测数据

    http://blog.csdn.net/pipisorry/article/details/52599451 基础知识 数据缺失的三种情形: 数据的似然和观测模型 Note: MLE中是将联合概率P ...

  6. Studio 一些使用

    1,配置: W:\android_tools\AndroidStudio2.1.3_SDK\android-studio-ide-141.2456560-windows\android-studio\ ...

  7. Android在一个TextView里显示不同样式的字体

    在同一个TextView里显示不同样式的字体 public void setSpan(Object what, int start, int end, int flags); 样式1:背景色.粗体.字 ...

  8. 【移动开发】binder阻塞/非阻塞与单向/双向的问题

    The client thread calling transact is blocked by default until onTransact has finishedexecuting on t ...

  9. Linux内核分配内存的方式

    page = alloc_pages(GFP_KERNEL, get_order(1234));分配失败返回NULLGFP_KERNEL  ---> 分配标志,当没有足够内存分配时,睡眠阻塞,直 ...

  10. printk的用法

    printk的用法 内核通过 printk() 输出的信息具有日志级别,日志级别是通过在 printk() 输出的字符串前加一个带尖括号的整数来控制的,如 printk("<6> ...