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. Spark中自定义累加器Accumulator

    1. 自定义累加器 自定义累加器需要继承AccumulatorParam,实现addInPlace和zero方法. 例1:实现Long类型的累加器 object LongAccumulatorPara ...

  2. linux信息查看手记

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...

  3. SSH基本概念和用途

    一.SSH是什么 简单的说,SSH是一种网络协议,主要用于客户端与远程主机的安全链接和交互. 二.常见用法1.客户端与远程主机的安全链接命令如下: $ ssh -p user@host 解释如下:-p ...

  4. 结对练习——Caculator

    本人:学号201521031045,码云https://gitee.com/L_Name 小伙伴:暂无 项目fork: https://gitee.com/L_Name/Calculator CalS ...

  5. Js的小技巧

    感谢好友破狼提供的这篇好文章,也感谢写这些知识点的作者们和将他们整理到一起的作者.这是github上的一篇文章,在这里本兽也就只做翻译,由于本兽英语水平和编程能力都不咋地,如有不好的地方也请多理解体谅 ...

  6. SQL 工具系列一

    1.误删除数据恢复篇 ApexSQL Recover   可以恢复Delete Truncate  drop,恢复 二进制大型对象 测试版本  每10行才会恢复 评估版本下载地址:只能用14天 所以基 ...

  7. CSS之APP开发比较实用的CSS属性

    简介:本人刚入前端没多久,在做APP的开发的时候,经常遇到一些奇怪的问题,本人经验少,会使用js来解决css上的问题,但,却不知道其实有些css已经帮我们解决了. 1,white-space: now ...

  8. SSH框架整合中Hibernate实现Dao层常用结构

    一.疑惑 一直以来,我在使用SSH框架的时候经常会发现后者有疑虑到底使用hibernate的那种方法或者如何配置hibernate来操作数据库,经过 一段时间的学习下面我来总结一下,常用的dao层配置 ...

  9. 很多个java面试题

    1. 为什么说Java是一门平台无关语言? 平台无关实际的含义是“一次编写到处运行”.Java 能够做到是因为它的字节码(byte code)可以运行在任何操作系统上,与底层系统无关. 2. 为什么 ...

  10. JavaScript常用类

    JS常用类 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | -Infinity 2.常用进制 二进制:0b1010 八进制 ...