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.

解题:

模拟乘法运算,可以完全按照模拟的思路,用num1的每一位乘num2,得到的数组按位保存在结果字符串中,并不断更新。

先把字符串反转,在逻辑上思路会更加清晰,当然不反转也可以。

 class Solution {
public:
string multiply(string num1, string num2) {
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
int l1 = num1.size();
int l2 = num2.size();
string res(l1 + l2, '');
int carry, d; for (int i = ; i < l1; ++i) {
int n1 = num1[i] - '';
carry = ;
for (int j = ; j < l2; ++j) {
int n2 = num2[j] - '';
d = n1 * n2 + carry + (res[i+j] - '');
carry = d / ;
res[i+j] = '' + (d - carry * );
} int idx = ;
while (carry != ) {
d = (res[i+l2+idx] - '') + carry;
carry = d / ;
res[i+l2] = '' + (d - carry * );
idx++;
}
} while (!res.empty() && res.back() == '')
res.pop_back();
if (res.empty())
return "";
reverse(res.begin(), res.end());
return res;
}
};

【Leetcode】【Medium】Multiply Strings的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode题意分析&解答】43. Multiply Strings

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

  6. 【LeetCode每天一题】Multiply Strings(字符串乘法)

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

  7. 【leetcode刷题笔记】Multiply Strings

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

  8. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

随机推荐

  1. python中 datetime模块的详解(转载)

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  2. TSM_ISSUE_123

    dsmc 命令详解http://www-ik.fzk.de/~apel/html/adsm_manual.htmlTSM InfoCenterhttp://www-01.ibm.com/support ...

  3. 案例25-servlet的抽取

    1 product模块的抽取版本一 1 ProductServlet代码 抽取之后,原来对应的IndexServlet,ProductListByCidServlet等都可以删除.对应的web.xml ...

  4. opensuse13.1 安装chrome报 error while loading shared libraries:libudev.so.0:cannot open shared object file:no file or directory

    1  opensuse13.1 安装chrome时 先用rpm -ivh --test **.rpm 测试安装  安装上缺少的文件 2 但是安装测试通过 却不能启动 原因 缺少一个文件 libudev ...

  5. css3毛玻璃效果白边问题

    注:css3毛玻璃效果应该很多人都知道怎么实现,但是有个问题是图片模糊了之后相当于缩小了,所以颜色深的图片会出现白边,这里说下我参考网上的解决方式吧! 1.毛玻璃实现方法: CSS3 blur滤镜实现 ...

  6. log4j.properties 基本配置

    log4j.rootLogger=WARN,stdout,D log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender ...

  7. 查找CPU使用率过高的线程

    1.在编写程序中有时候设置不恰当,休眠时间不够,一般情况下4核的电脑CPU使用率一直大于23%,8核的大于13%就有可能是这种情况 解决方法: 在VS查看并行线程利用CPU使用工具ProcessExp ...

  8. show_space

    create or replace procedure show_space( p_segname_1 in varchar2,p_space in varchar2 default 'AUTO',p ...

  9. C++(笔)001.

    1.编程范式 编程范式是指计算机编程的基本风格,C++可容纳多种程度范式,如面向对象编程.泛型编程及传统的过程式编程. 2.与C相比较 C++在C语言的基础上新加的特性如下: a.类和对象.继承 b. ...

  10. node.js搭建https服务器

    HTTPS简介 HTTPS:(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版. ...