leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
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(大整数运算)的更多相关文章
- Multiply Strings大整数乘法
[抄题]: 以字符串的形式给定两个非负整数 num1 和 num2,返回 num1 和 num2 的乘积. [暴力解法]: 时间分析: 空间分析: [思维问题]: 还要找到结果中第一位不等于0的数再添 ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- Add Strings大整数加法十进制求和 & Add Binary二进制求和
[抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...
- LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法
题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2" ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
随机推荐
- mysql deadlock处理
1.SHOW ENGINE INNODB STATUS 得到最后一次死锁发生的状况 =====================================140110 11:43:07 INNOD ...
- java信号量PV操作 解决生产者-消费者问题
package test1; /** * 该例子演示生产者和消费者的问题(设只有一个缓存空间.一个消费者和一个生产者) * MySystem类定义了缓冲区个数以及信号量 * @author HYY * ...
- 【无聊放个模板系列】POJ 3678 2-SAT
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...
- [数论]ZOJ3593 One Person Game
题意:一个人要从A走到B 只能走a布.b步.(a+b)步,可以往左或右走 问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...
- 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
当 Adobe.Microsoft.Sun 等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 现今存在的开源协议很多,而经过 Open Source Initiative 组织通过批 ...
- Android 开发之 ---- 底层驱动开发(一)
驱动概述 说到 android 驱动是离不开 Linux 驱动的.Android 内核采用的是 Linux2.6 内核 (最近Linux 3.3 已经包含了一些 Android 代码).但 Andro ...
- Git教程(2)官方命令文档及常用命令表
http://www.cnblogs.com/angeldevil/archive/2013/11/26/3238470.html 1,官方命令文档 http://www.git-scm.com/do ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)4-----Net下的AppDomain编程 [摘录]
最近在对AppDomain编程时遇到了一个问题,卸载AppDomain后,在内存中还保留它加载的DLL的数据,所以即使卸载掉AppDomain,还是无法更新它加载的DLL.看来只有关闭整个进程来更新D ...
- ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引
ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引 因为表KOL_XX_FIN050_TEMP 为临时表,而且有其他session正在使用. 处理步骤: 1.先从 dba ...
- MySQL timed_mutexes
提要: MySQL 5.5.39 Release版本正式从源码里删除了全局参数timed_mutexes.timed_mutexes原本用来控制是否对Innodb引擎的mutex wait进行 计时统 ...