[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 ...
随机推荐
- OpenCV学习(一)基础篇
OpenCV 2 计算机视觉编程手册读书笔记1 矩阵创建 Mat类是OpenCV中非常有用类,用来创建和操作多维矩阵.可以有很多方法构造它. // 构造函数 //! constructs 2D mat ...
- JAVA学习笔记系列1-Java版本介绍
JavaSE(Java Standard Edition):标准版,定位在个人计算机上的应用(桌面应用).因为一般都是Windows系统,因此Java的这个发展并不好. JavaEE(Java Ent ...
- vue 使用vux封装的微信分享
main.js引入 import {WechatPlugin} from 'vux' Vue.use(WechatPlugin) 公共的jswxShare.js import Vue from 'vu ...
- Java多线程入门中几个常用的方法
一.currentThread()方法 currentThread方法就是返回当前被调用的线程. 该方法为一个本地方法,原码如下: /** * Returns a reference to the c ...
- 添加mtdparts引起的问题
今天在给uboot添加分区,大家都知道添加完之后直接在终端里面mtd会报错: SMDK2440 # mtd mtdparts variable not set, see 'help mtdparts' ...
- 看淘宝营销api 文档有感
total: use appkey & secrect variance naming rule 提供沙箱环境 使用api gateway 使用rest(但返回结果包裹了 isp.thread ...
- Socket基础之-启动异步服务侦听
Socket网络编程第一篇: 本文主要是以代码为主. .NET技术交流群 199281001 .欢迎加入 1 //负责监听的套接字 private Socket socketServer; //通知一 ...
- 【转】python两个 list 获取交集,并集,差集的方法
1. 获取两个list 的交集: #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] ...
- python, Image
PIL: Python Image Library, python平台的图像处理库,要使用Image首先要从PIL库导入Image: from PIL import Image 如果没有安装PIL的包 ...
- 十五、springcloud(一)注册中心Eureka
1.Eureka的基本架构 a.Eureka Server 提供服务注册和发现 b.Service Provider 服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到 c.Servi ...