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. SOME USEFUL MACHINE LEARNING LIBRARIES.

    from: http://www.erogol.com/broad-view-machine-learning-libraries/ http://www.slideshare.net/Vincenz ...

  2. 自定义CSS博客(转)

    摘自:http://www.cnblogs.com/libaoheng/archive/2012/03/19/2406836.html 前  言 一个好的阅读体验,对技术博客来说,也许算是锦上添花.入 ...

  3. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  4. GATK3.2.2小结(转载)

    http://blog.csdn.net/skenoy/article/details/38346489 经过几天的摸索和网上资料的查询对GATK软件有点小心得,现总结如下: 1. fasta文件最好 ...

  5. iOS - OC Struct 结构体

    1.结构体的定义与调用 // 定义结构体类型 // 结构体类型名为 MyDate1 struct MyDate1 { int year; int month; int day; }; // 定义结构体 ...

  6. 09 redo and undo

    本章提要-----------------------------------------------redo, undo 定义redo, undo 如何工作如何访问 redo, undo提交和回滚- ...

  7. css 常用样式命名规则

    大家在写css的时候,对一些html标签起一个合适的名字是个很头疼的事情,现在给大家分享项目中常用的名字供参考. 外套:wrap  ——用于最外层 头部:header  ——用于头部 主要内容:mai ...

  8. JavaWeb学习总结(三)—Servlet

    1. 什么是Servlet * Servlet是JavaWeb三大组件之一(Servlet.Filter.Listener) ,Servlet是用来处理客户端请求的动态资源,按照一种约定俗成的称呼习惯 ...

  9. (三)主控板生级uboot与内核

    一.升级uboot 把我的电脑增加一个192.168.1.3的ip,方便升级boot. 1.连接串口,连接网口到GE1; 2.登陆电脑后到D:\MTG3000MAIN\first目录打开tftp32 ...

  10. eclipse右击打war包class没打上去的问题

    今天,遇到个诡异的问题,一个工程eclipse打war包class就是打不上,别的能打上,发现这样选择tomcat,打包成功,谨此备注.