一天一道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. PHP 安全 E-mail

    PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> <body> <?php if (isset($_REQUEST['email'])) / ...

  2. Java对象的创建 —— new之后JVM都做了什么?

    Java对象创建过程 1. 类加载检查 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没 ...

  3. MacOS下安装rvm的几点注意

    如果用以下链接无法下载的话: curl -sSL https://get.rvm.io | bash -s stable #或者 curl -L https://rvm.io | bash -s st ...

  4. 如何找出Xcode中不同版本Swift的路径

    我们知道Xcode中可能包含不知一个Swift的版本,那么我们如何找到它们对应的路径呢? 熟悉unix shell命令的童鞋都知道有一个find指令,在我们已知Xcode路径时,我们可以在其中找到Sw ...

  5. 4-sum问题

    给定一个整数数组,判断能否从中找出4个数a.b.c.d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对. #define SIZE 10 void judgeAndPut(int* arr, ...

  6. Java学习之栈和堆的区别

    在函数中定义的一些基本类型的变量和对象的引用变量都在函数的栈内存中分配. 当在一段代码块定义一个变量时,Java就在栈中为这个变量分配内存空间,当超过变量的作用域后,Java会自动释放掉为该变量所分配 ...

  7. Spark技术内幕: Shuffle详解(一)

    通过上面一系列文章,我们知道在集群启动时,在Standalone模式下,Worker会向Master注册,使得Master可以感知进而管理整个集群:Master通过借助ZK,可以简单的实现HA:而应用 ...

  8. Java的访问权限详解(3+1)public private protected default

    Java使用三个关键字在类的内部设定访问权限:public.private.protected.这些访问指定词(access specifier)决定了紧跟其后被定义的成员(方法或属性)可以被谁使用. ...

  9. Android初级教程Fragment到Fragment的通信初探

    这里只是给出三个类RightFragment.LeftFragment.MainActivity中的简易代码,至于布局怎么设定,不做赘述. 思路:从碎片一获取与之依托的活动实例,碎片一可以调用活动里面 ...

  10. StringBuffer与StringBuilder详解

    刚刚在参加网易实习生在线考试的时候,出了一道选择题谈到了StringBuilder这个类的一些选项,虽然那道题自己做对了,但是也提醒了我应该好好了解一些StringBuffer与StringBuild ...