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 ...
随机推荐
- Firefly的角色跳转场景简单示例
源地址:http://bbs.9miao.com/thread-45790-1-2.html 本例演示的是模拟游戏服务端,让角色在场景1中跳转到场景2中.在实际游戏中,client将要跳转的角色id和 ...
- tomcat配置301重定向(urlRewrite URL重写)
tomcat默认情况下不带www的域名是不会跳转到带www的域名的,而且也无法像apache那样通过配置.htaccess来实现.如果想要把不带“www'的域名重定向到带”www"域名下,又 ...
- 使用eclipse远程调试Tomcat的方法
tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢? 1. 在eclispe中新建we ...
- photoshop:制作木板木纹
1.设置颜色为木头相近颜色 2.滤镜->渲染->云彩 3.滤镜->杂色->添加杂色 4.滤镜->模糊->动感模糊 5.用矩形选取选取某块区域 6.滤镜->扭曲 ...
- LCMS
LCMS(LearningContent Management System) 即学习内容管理系统
- 性能优化工具 MVC Mini Profiler
性能优化工具 MVC Mini Profiler MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC.WebForm 以及WCF 的性能分析的小程 ...
- How to learn C++ and find all STL Algorithm reference
You can find all cpp references on websites: http://zh.cppreference.com/ http://www.cplusplus.com/re ...
- android系统平台显示驱动开发简要:Samsung LCD接口篇『三』
平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...
- 【HDOJ】4601 Letter Tree
挺有意思的一道题,思路肯定是将图转化为Trie树,这样可以求得字典序.然后,按照trie的层次求解.一直wa的原因在于将树转化为线性数据结构时要从原树遍历,从trie遍历就会wa.不同结点可能映射为t ...
- 【HDOJ】4122 Alice's mooncake shop
RMQ的基础题目,简单题. /* 4122 */ #include <iostream> #include <sstream> #include <string> ...