https://leetcode.com/problems/multiply-strings/

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.

class Solution {
public:
string multiply(string num1, string num2) {
vector<int> a, b; for(int i=num1.length()-;i>=;--i) a.push_back(num1[i] - '');
for(int i=num2.length()-;i>=;--i) b.push_back(num2[i] - ''); vector<int> res(a.size()+b.size());
for(int i=;i<res.size();++i) res[i] = ; for(int i=;i<a.size();++i) {
for(int j=;j<b.size();++j) {
res[i+j] += a[i] * b[j];
}
} for(int i=;i<res.size();++i) {
if(res[i]>=) {
res[i+] += res[i]/;
res[i] = res[i]%;
}
}
//for(int i=0;i<res.size();++i) cout<<res[i]<<" ";
//cout<<endl;
while(res[res.size()-] == ) {
if(res.size() == ) break;
res.pop_back();
} string sres = "";
for(int i=res.size()-;i>=;--i) sres += res[i] + '';
return sres;
}
};

https://leetcode.com/problems/add-two-numbers/

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
vector<int> na; na.clear();
vector<int> nb; nb.clear(); while(l1) {
na.push_back(l1->val);
l1 = l1->next;
} while(l2) {
nb.push_back(l2->val);
l2 = l2->next;
} vector<int> sum(max(na.size(), nb.size()) + );
for(int i=;i<sum.size();++i) sum[i] = ; int i=;
for(;i<min(na.size(), nb.size());++i) sum[i] = na[i] + nb[i];
for(;i<max(na.size(), nb.size());++i) sum[i] = max(na.size(), nb.size())==na.size() ? na[i]: nb[i]; for(int i=;i<sum.size()-;++i) {
if(sum[i] >= ) {
sum[i+] += sum[i] / ;
sum[i] = sum[i] % ;
}
} if(sum[sum.size()-] == ) sum.pop_back(); ListNode *head = new ListNode(-);
ListNode *r = head, *s;
for(int i=;i<sum.size();++i) {
s = new ListNode(-);
s->val = sum[i];
r->next = s;
r = s;
}r->next = NULL; head = head->next; return head;
}
};

leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)的更多相关文章

  1. Multiply Strings大整数乘法

    [抄题]: 以字符串的形式给定两个非负整数 num1 和 num2,返回 num1 和 num2 的乘积. [暴力解法]: 时间分析: 空间分析: [思维问题]: 还要找到结果中第一位不等于0的数再添 ...

  2. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  3. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  7. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

  8. LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法

    题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2" ...

  9. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

随机推荐

  1. hdu 4035 Maze 概率DP

        题意:    有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树,    从结点1出发,开始走,在每个结点i都有3种可能:        1.被杀死,回到结点1处(概率为ki)      ...

  2. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  3. 深入php面向对象和模式

    前两章是php历史和概论,略过. 第三章 对象基础 3.1 类和对象 类,是用于生成对象的代码模版. public 公有的,都可调用. protected 保护的, 只有本类和子类可以调用. priv ...

  4. bochs编译安装

    1.安装依赖sudo apt-get install gtk2.0-devsudo apt-get install xorg-dev 2.配置 ./configure --enable-debugge ...

  5. 存储过程 务的概念 事务的特性 关于异常的处理 连接池 构JdbcUtil类

    1 存储过程    1)用当地数据库语言,写的一段业务逻辑算法,并该算法存储在客户端    2)使用存储过程需要用于CallableStatement接口,同时需要使如下SQL命令调用:{call a ...

  6. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)4-----Net下的AppDomain编程 [摘录]

    最近在对AppDomain编程时遇到了一个问题,卸载AppDomain后,在内存中还保留它加载的DLL的数据,所以即使卸载掉AppDomain,还是无法更新它加载的DLL.看来只有关闭整个进程来更新D ...

  7. poj3270Cow Sorting(置换)

    链接 对于组合数学是一点也不了解 讲解 重要一点 要知道一个循环里最少的交换次数是m-1次 . #include <iostream> #include<cstdio> #in ...

  8. js模仿jquery里的几个方法parent, parentUntil, children

    有时工作需要, 也是接着上一章的方法, 用js模仿jquery里的几个方法parent, parentUntil, children. function parent(node){ return no ...

  9. foreach中引用 的问题

    在工作中遇到 关于 php foreach 引用的一个问题 简单来说,如下代码 $arr=array('a','b','c' ,'d'); foreach($arr as $k=>&$v ...

  10. UML中常见关系详解(泛化、实现、依赖、关联、组合、聚合)

    UML中类与类,已经类与接口,接口与接口的关系有:泛化(generalization),关联(association),依赖(dependency),实现(realization)这几种.   泛化( ...