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 {
public:
string multiply(string num1, string num2) {
}
};

我的解法是每次提取num2的一位,然后和num1相乘,所得结果并入string res中,这样虽然每次都要新定义一个string,效率略低,但是思路比较清晰:把字符串相乘划分成了“字符串和数字相乘” 和 “字符串相加” 两个子问题,分别解决就可以。

处理时先将num1,和num2逆序,使得最低位在[0] 位置,这样处理起来比较方便,不易写错。

class Solution {
public:
string multiply(string num1, string num2) {
if(num1.length() == || num2.length() == )
return "";
if(num1.length() == && num1[] == '') return "";
if(num2.length() == && num2[] == '') return "";
string res(num1.length() + num2.length(), '');
int i = , j = ;
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
for(i = ; i < num2.length(); ++i){
int offset = i;
string tmp(offset + num1.length() + , '');
int add = ;
for(j = ; j < num1.length(); ++j){
int tmpMul = (num1[j] - '') * (num2[i] - '') + add;
add = tmpMul / ;
tmp[j + offset] = (tmpMul % ) + '';
}
tmp[j + offset] = add + '';
plus(res, tmp);
}
int countStartZero = ; //高位'0'的个数
for(i = res.length() - ; i >= && res[i] == ''; --i, ++countStartZero);
if(countStartZero == res.length()) return "";
res = res.substr(, res.length() - countStartZero);
reverse(res.begin(), res.end());
return res;
}
private:
void plus(string &s1, string &s2){
int add = ;
for(int i = ; i < s1.length() && (add > || i < s2.length()); ++i){
int tmp = (s1[i] - '') + add;
if(i < s2.length()) tmp += (s2[i] - '');
add = tmp / ;
s1[i] = (tmp % ) + '';
}
}
};

[LeetCode] 大数问题,相加和相乘,题 Multiply Strings的更多相关文章

  1. LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法

    题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2" ...

  2. leetcode 第42题 Multiply Strings

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

  3. Leetcode——258.各位相加【水题】

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

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

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

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

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

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

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

  7. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. leetcode面试准备:Multiply Strings

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

随机推荐

  1. LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)

    题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...

  2. Linux内核设计笔记12——内存管理

    内存管理学习笔记 页 页是内核管理内存的基本单位,内存管理单元(MMU,管理内存并把虚拟地址转化为物理地址的硬件)通常以页为单位进行处理,从虚拟内存的角度看,页就是最小单位. struct page{ ...

  3. angularJS遇到的坑

    最近在用angularjs做一些东西,由于学艺不精,对angularjs了解不够,导致经常会不小心掉进一些自己挖的坑里(⊙_⊙),在这里记下来,谨防又踩. 1.angularjs ng-show no ...

  4. bootstrapValidator.js,最好用的bootstrap表单验证插件 简单实用方法

    实用方法 1.引入 在有jquery和bootstrap的页面里引入bootstrapValidator.js和bootstrapValidator.css文件 2. 按照bootstrap的表单组件 ...

  5. 动态规划——最长上升子序列LIS及模板

    LIS定义 一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, …, aN),我们可以得到一些上升的子序列(ai1 ...

  6. Sum of Consecutive Prime Numbers(素数打表+尺取)

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  7. 《梦断代码Dreaming In Code》阅读计划

    书籍是人类宝贵的精神财富,读书是人们重要的学习方式,是人生奋斗的航灯,是文化传承的通道,是人类进步的阶梯.学生作为学习人群的主体,必须把读书作为头等大事.学校就是一个学生在教师指导下自主读书的空间,而 ...

  8. SOA架构的理解

    实践论认为:从实践提升到理论,再由理论指导实践,由此向前发展.目前SOA的发展的情况………… 通过不少实践,SOA的模型己经被公认为标准规范,目前是正需要进一步总结上升到理论的时候了. SOA架构的演 ...

  9. 判断两个字符串是否相等【JAVA】

    if(A.equals(B)){ } 之前总是用"=="来判断,但是在JAVA里面好像不行.所以,用equals(). 查了下资料. 原因:equal()比较的是对象的内容,&qu ...

  10. 工具函数:cookie的添加、获取、删除

    cookie是浏览器存储的命名数据,作用是保存用户的信息,这样我们就可以用这些信息来做一些事了,但是cookie容量很小,只有4kb. 下面是我总结的cookie的添加.获取.删除的函数: cooki ...