[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 ...
随机推荐
- eclipse工具类及插件(Eclipse超好用的插件推荐)
https://blog.csdn.net/ghostxbh/article/details/80054948
- 《JavaScript Dom 编程艺术》读书笔记-第5章
上一篇随笔中记录了用JavaScript建一个基础图片库,但实际上还有很多地方可以改进.第五章将逐步进行改进,这一章里需要明白的道理是达到目标的过程和达到目标同样重要~ 第五章:最佳实践 5.1 过去 ...
- c#抓屏功能在DPI缩放后,截到的图片不完整的问题
/// <summary> /// 获取屏幕快照 /// </summary> /// <returns></returns> public stati ...
- Curl追踪请求延时问题
背景原因:测试环境发现一个连接内网访问和外网访问延迟差别很大,内网访问很快.外网访问很慢.于是我们用curl来诊断问题所在的区域! 命令如下: curl -o /dev/null -s -w %{ti ...
- 20155208徐子涵 《网络对抗》Exp1 PC平台逆向破解
20155208徐子涵 <网络对抗>Exp1 PC平台逆向破解 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数 ...
- Arch Linux 的休眠设置
https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate_(简体中文)https://wiki.archl ...
- container and Injection
1.容器的历史 容器概念始于 1979 年提出的 UNIX chroot,它是一个 UNIX 操作系统的系统调用,将一个进程及其子进程的根目录改变到文件系统中的一个新位置,让这些进程只能访问到这个新的 ...
- 【java多线程】队列系统之LinkedBlockingDeque源码
1.简介 上一篇我们介绍了 LinkedBlockingDeque 的兄弟篇 LinkedBlockingQueue .听名字也知道一个实现了 Queue 接口,一个实现了 Deque 接口,由于 D ...
- poj2279——Mr. Young's Picture Permutations
Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...
- json序列化时定制支持datetime类型,和到中文让他保留中文形式
json序列化时,可以处理的数据类型有哪些?如何定制支持datetime类型 自定义时间序列化转换器 import json from json import JSONEncoder from dat ...