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 ...
随机推荐
- kafka.network.AbstractServerThread中的线程协作机制
这个虚类是kafka.network.Acceptor和kafka.network.Processor的父类,提供了一个抽象的Sever线程. 它的有趣之处在于为子类的启动和停止提供了线程间的协作机制 ...
- 李洪强iOS开发之自定义cell的使用
第一步: 创建自定义cell类,继承自UItableVIewcell 第二步: 在sb中布局自己需要的视图控件并且将此cell与我刚刚创建的cell类进行关联.并且连线 第三步: 创建modle类, ...
- cocos2d-x 锚点,位置==》动手实验记录 多动手... :)
总结: 1:cocos2d-x的位置由锚点和Position位置 共同决定的.2: cocos2d-x,当位置不设置或者为零的时候, 子节点的锚点位置永远位于父节点的左小角的地方3:我们的自己的游戏编 ...
- 函数可重入问题reentrant functions(函数执行过程中可以被中断,允许多个副本)
最近经常听到这个名词,以前也听到过,不过接触更多的是“线程安全问题”,而且本人也一直理解的是两个名字的含义是一样的.今天仔细总结一下这个名词相关的概念. 引用博文:可重入函数和不可重入函数 (http ...
- How to Send an HTTP Header With Every Request With Spring RestTemplate
In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...
- 判断浏览器类型-----------navigator.userAgent.indexOf()
<script language="JavaScript"> <!-- function getOs() { var OsObject = "" ...
- [原]Unity3D深入浅出 - 脚本开发基础(Scripts)
常用脚本事件: Update:每帧调用一次 Start:在第一次Update执行前调用 Awake:脚本实例在创建时调用 FixedUpdate:每个固定物理时间间隔调用一次 LateUpdate:每 ...
- UVa 10213 (欧拉公式+Java大数) How Many Pieces of Land ?
题意: 一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 分析: 首先紫书上的公式是错的,不过根据书上提供的思路很容易稍加修改得到正确答案! 然后推公式吧,这里用到平面图的欧拉公 ...
- windows远程桌面3389超时锁定时间调整方法(取消锁屏时间限制)
我们在管理服务器操作时,有时候需要长时间操作服务器,有时候稍微离开下倒杯水或接个稍长点的电话,就超时断开了很烦啦!有没有方法解决这个问题类?答案是有的!我只要在组策略里面,稍微修改下超时时间就可以了. ...
- ARCGIS10如何修改图例的大小
设置好图例的样式,然后转换成图形,接着ungroup,全部打散,就可以对每一个图形包括文字进行大小和格式的编辑