Problem Description http://oj.leetcode.com/problems/multiply-strings/

Basic idea is to multiply two nums like we do caculation on paper, one by one digital multiplication, then add the temporary results together to get the final one. Be careful about the last carry digital.

 class Solution {
public:
string multiply_one_digital(char c, string num2, int tail){
string result;
for(int i = ; i < tail; i ++) {
result.push_back('');
} int carry = ;
for(int i = num2.size() - ; i >= ; i-- ) {
int one_result = (c - ) * (num2[i] - ) + carry;
carry = one_result/;
char current_digital = one_result % + ;
result.insert(result.begin(),current_digital);
} if(carry > )
result.insert(result.begin(), (char)(carry + )); return result;
} string multiply(string num1, string num2) {
// Note: The Solution object is instantiated only once and is reused by each test case.
string result;
if(num1.size() == || num2.size() == )
return result;
if((num1.size() == && num1[] == '') ||
(num2.size() == && num2[] == '')) {
result.push_back('');
return result;
} vector<string> tmp_results;
for(int i = num1.size() - ; i >= ; i -- ) {
if(num1[i] == '')
continue; tmp_results.push_back(multiply_one_digital(num1[i], num2, num1.size() - - i));
} //add all temporary results
string last_tmp_result = tmp_results[tmp_results.size() - ];
int carry = ;
for(int i = ; i < last_tmp_result.size(); i ++ ){
int one_result = ;
for( auto item: tmp_results) {
if(i > item.size() - )
continue; one_result += (item[item.size() - - i] - );
} one_result += carry;
carry = one_result/;
char current_digital = one_result % + ;
result.insert(result.begin(),current_digital);
} if(carry > )
result.insert(result.begin(), (char)(carry + )); return result;
}
};

Multiply Strings [LeetCode]的更多相关文章

  1. Multiply Strings leetcode java

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

  2. leetcode面试准备:Multiply Strings

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

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

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

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

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

  5. 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) ...

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

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

  7. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  8. 【LeetCode练习题】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

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

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

随机推荐

  1. 通过NuGet获取sqlite对应的.net的dll

    https://www.nuget.org/packages/System.Data.SQLite/ 直接在Package Manager Console中执行命令,会自动安装依赖项的 Install ...

  2. 让PHP 5.3支持MSSQL连接

    最近在Windows Server 2008配置了PHP环境,想要连接SQL Server 2008,但是悲催的发现,从5.3开始,PHP自带的dll不再支持2000以后的MS SQL Server了 ...

  3. 目前比较全的CSS重设(reset)方法总结

    在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...

  4. 多命令顺序执行、管道符 ; && || |

    多命令顺序执行:

  5. JavaWEB 常用开发模式MVC+三层结构

    MVC开发模式: M:  Model   -- JavaBean C:  Controler   --  Servlet V: View   --- JSP 不会在word里面画画,所以就直接截了 老 ...

  6. asp.netMVC4(基础知识----传值问题分析)

    (1)一般在数据交互的时候,都会涉及到前后台间的相互传值,一般的情况下,方法也有多种,下面就后台定义变量往前台传值: 以下是后台代码: /// <summary> /// 展示举报信息 / ...

  7. Java初始化(构造器)

    在类的内部,变量定义的先后顺序决定了初始化的顺序.即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化. import static humeng.com.cnblo ...

  8. 巧妙的实现 CSS 斜线(炫酷的小效果)

      开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果 ...

  9. 如何查看与刷新DNS本地缓存

    如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...

  10. 【CDN】国外访问国内服务器网站-响应慢-CDN

    建议采用CDN海外加速方式: (1)CDN即内容分发网络(Content Delievery Network),它可以认为是建立在现有IP网络基础结构之上的一种增值网络.CDN技术将多点负载均衡.镜像 ...