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. Linux下Nginx+Tomcat整合的安装与配置

    因为nginx处理静态页面的速度很快,并且是免费的,它还可以配置负载均衡的服务器集群来搭建多个tomcat,所以nginx+tomcat是企业搭 建javaee项目很好的选择.nginx主要是通过反向 ...

  2. Oracl 动态执行表不可访问,本会话的自动统计被禁止

    oracle ---建立SQL窗体 写入 select * from tableA; 弹出错误窗口 : 动态执行表不可访问,本会话的自动统计被禁止.在执行菜单里你可以禁止统计,或在v$session, ...

  3. CodeForces114E——Double Happiness(素数二次筛选)

    Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers ...

  4. CVE爬虫抓取漏洞URL

    String url1="http://www.cnnvd.org.cn/vulnerability/index/vulcode2/tomcat/vulcode/tomcat/cnnvdid ...

  5. Java API ——Arrays类

    1.Arrays类概述 · 针对数组进行操作的工具类. · 提供了排序,查找等功能. 2.成员方法 · public static String toString(int[] a):in[] a可以改 ...

  6. ColorBox常见问题

    发现colorbox官方网站的troubleshoot写的比较好,转载一下. 1,flash覆盖colorbox: This is not a ColorBox specific problem, b ...

  7. 怎样开发Chrome浏览器的插件

    http://jingyan.baidu.com/article/b907e627fb90fd46e7891c3c.html Chrome 浏览器作为基于Webkit的新一代浏览器.Chrome自从正 ...

  8. JS计算字符串所占字节数

    最近项目有个需求要用js计算一串字符串写入到localStorage里所占的内存,众所周知的,js是使用Unicode编码的.而Unicode的实现有N种,其中用的最多的就是UTF-8和UTF-16. ...

  9. 【HDOJ】4043 FXTZ II

    1. 题目描述有n个球,第i个球的伤害值为$2^i-1, i \in [1,n]$.有甲乙两个人,每次由甲选择n个球中的一个,用它以相同概率攻击自己或者乙,同时彻底消耗这个球.这样的攻击最多进行n次. ...

  10. poj 1328 Radar Installation(贪心)

    题目:http://poj.org/problem?id=1328   题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...