[leetcode]2. Add Two Numbers.cpp
You are given two non-empty linked lists representing two non-negative integers. 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. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题目说给两个链表,其中是两个整数的逆序单数字串,求两个相加再逆序的链表。
例子说 (2->4->3)+(5->6->4) ==> 342+465=807 ==> 7->0->8
理解了题目就很好做了,类似大数相加的方法,一个一个加过去,设个cn保存一下进位,最后再处理一下cn,每次相加就直接创建一个新node放进去。
//
// Created by x on 2017/6/30.
//
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* n1=l1;
ListNode* n2=l2;
vector<int> qe_re;
int a,b,cn,re;
a=b=cn=re=;
while(n1!=NULL || n2!=NULL){
a=n1!=NULL?n1->val:;
b=n2!=NULL?n2->val:;
if(n1!=NULL) n1=n1->next;
if(n2!=NULL) n2=n2->next;
re=a+b+cn;
cn=re/;
re=re%;
qe_re.push_back(re);
}
if(cn!=)
qe_re.push_back(cn);
ListNode *result;
if(qe_re.size()==)
return result;
else{
result = new ListNode(qe_re[]);
}
ListNode *next = result;
for(int i=;i<qe_re.size();i++){
next->next = new ListNode(qe_re[i]);
next=next->next;
}
return result;
} int main(){
ListNode *p1=new ListNode();
p1->next= new ListNode();
ListNode *p2=new ListNode();
ListNode *p = addTwoNumbers(p1,p2); return ;
}
[leetcode]2. Add Two Numbers.cpp的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
随机推荐
- 为什么ssh 执行完命令以后 挂了, hang , stop respond
- LNMP(一)
第二十课LNMP(一) 目录 一.LNMP架构介绍 二.MySQL安装 三.PHP安装 四.Nginx介绍 五.Nginx安装 六.扩展 一.LNMP架构介绍 之前已经学习过LAMP架构,与LAMP相 ...
- 20175224 2018-2019-2 《Java程序设计》第二周学习总结
教材学习内容总结 本周对教材的第二第三章进行了学习,通过阅读教材,我发现java和c语言在相似的基础上还是有很多不同的地方,以下是我对这周学习知识的一些总结. 2.1 java标识符中的字母是区分大小 ...
- 重构 改善既有代码的设计 Replace Method with Method Object(以函数对象取代函数)
你有一个大型函数,其中对局部变量的使用使你无法采用Extract Method. 将这个函数放进一个单独对象中,如此一来局部变量就成了对象内的字段.然后你可以在同一个对象中将这个大型函数分解为多个小型 ...
- 剑指Offer 31. 整数中1出现的次数(从1到n整数中1出现的次数) (其他)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- 创建cocoapod静态库发布到网上使用
1,在github上创建一个空仓库,要有licence和readme,clone到本地 2,pod lib create 项目名,这样就创建了一个静态库 3,pod spec create 项目名,创 ...
- PHP之缓存雪崩,及解决方法(转)
一.什么是缓存雪崩缓存雪崩就是指缓存由于某些原因(比如 宕机.cache服务挂了或者不响应)整体crash掉了,导致大量请求到达后端数据库,从而导致数据库崩溃,整个系统崩溃,发生灾难. 下面的就是一个 ...
- 【linux基础】cuDNN版本查询
参考 1. 查看cudnn版本; 完
- 【转】Spring-boot 字符集设置 解决乱码方案
使用spring-boot开发时候,有时候程序没事,往往不经意会造成中文到前端变成乱码(????这样情况) 下面给出spring-boot项目统一字符集设置方案: 1.Spring Boot修改编码方 ...
- 我的代码-data pulling
# coding: utf-8 import datetimeimport timefrom sqlalchemy.engine import create_enginefrom sqlalchemy ...