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. jquery之event与originalEvent的关系、event事件对象用法浅析

    在jquery中,最终传入事件处理程序的 event 其实已经被 jQuery 做过标准化处理, 其原有的事件对象则被保存于 event 对象的 originalEvent 属性之中, 每个 even ...

  2. 自动编号维护SNRO

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. Servlet&jsp基础:第三部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. [SAP ABAP开发技术总结]ABAP读写、解析XML文件

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. CUBRID学习笔记 37 ADO.NET Schema Provider

    通常需要添加以下引用:   1 2 3 using System.Data; using System.Data.Common; using CUBRID.Data.CUBRIDClient; 定义连 ...

  6. C语言程序设计现代方法1,2,3章

    1:浮点型(float)运算比int慢,并且可能存在舍入误差 如float存储0.1,以后使用可能会变成0.099999999987 2:宏定义只用大写,这是大多数C程序猿遵循的规范! C语言区分大小 ...

  7. 拓扑排序--UVa10305

    题目 Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortun ...

  8. nginx安装配置域名转发

    1.安装pcre 1.[root@localhost home]# tar zxvf pcre-8.10.tar.gz //解压缩 2.[root@localhost home]# cd pcre-8 ...

  9. 阿里云大数据三次技术突围:Greenplum、Hadoop和“飞天”

    阿里云大数据三次技术突围:Greenplum.Hadoop和"飞天"    对于企业来说,到底什么是云计算?相信很多企业都有这样的困惑,让我们一起回到这个原始的起点探讨究竟什么是云 ...

  10. HDU 1698

    成段更新 这是一种把 num[]上空结点当做lazy标志使用的方法 都一样... #include <stdio.h> #include <string.h> #include ...