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. 将文件读取到内存、打印pe结构

    #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h&g ...

  2. zoj 2107&&hdu 1007最近点对问题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds   ...

  3. Ubuntu 安装hadoop 伪分布式

    一.安装JDK  : http://www.cnblogs.com/E-star/p/4437788.html 二.配置SSH免密码登录1.安装所需软件        sudo apt-get ins ...

  4. SQL HAVING语句

    HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. SQL HAVING 语法 SELECT column_name, aggregate_f ...

  5. linux下文件系统类型的学习

    1. 以超级用户权限登陆Linux,进入  /lib/modules/2.6.32--504.el6.x86_64/kernel/fs目录执行 ls 命令(不同Linux发行版本的Fs目录有些不同你可 ...

  6. QQLogin

    import java.awt.*; import javax.swing.*; public class QQLogin extends JFrame{ QQLogin(){ this.setSiz ...

  7. bootstrap 图片轮播效果

    <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="http: ...

  8. PHPStorm怎么修改选中的背景颜色呢?

    File -> Settings -> editor ->color&fonts->general->下拉框中的selection background-> ...

  9. Android照相机模块编程 照片颠倒问题及查询摄像头参数问题的解决

    这两天编程弄Android照相机模块,设置好各种参数后,发现预览的时候,照片是颠倒了,不是上下颠倒而是颠倒90°. 我的手机是华为U9200,用的Android4.0.3,后来看到http://www ...

  10. D3.js 让图表动起来

    D3 支持制作动态的图表.有时候,图表的变化需要缓慢的发生,以便于让用户看清楚变化的过程,也能给用户不小的友好感. 一.什么是动态效果 绘制完成后不再发生变化的,这是静态的图表. 动态的图表,是指图表 ...