[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 ...
随机推荐
- 蓝牙协议分析(9)_BLE安全机制之LL Privacy
1. 前言 在上一篇文章[1]中,我们介绍了BLE的白名单机制,这是一种通过地址进行简单的访问控制的安全机制.同时我们也提到了,这种安全机制只防君子,不防小人,试想这样一种场景: A设备表示只信任B. ...
- 蓝牙协议分析(5)_BLE广播通信相关的技术分析
1. 前言 大家都知道,相比传统蓝牙,蓝牙低功耗(BLE)最大的突破就是加大了对广播通信(Advertising)的支持和利用.关于广播通信,通过“玩转BLE(1)_Eddystone beacon” ...
- yarn依赖管理工具的使用
Yarn是Facebook发布的一款依赖管理工具,它比npm更快.更高效. 与NPM命令对照 npm install => yarn install npm install --save [pa ...
- jQuery-1.样式篇
jQuery对象与DOM对象 对于才开始接触jQuery库的初学者,我们需要清楚认识一点: jQuery对象与DOM对象是不一样的 可能一时半会分不清楚哪些是jQuery对象,哪些是DOM对象,下面重 ...
- U-Boot shell脚本语法
/********************************************************************** * U-Boot shell脚本语法 * 说明: * 之 ...
- Windows下运行Linux命令
安装Gow软件,Gow-0.7.0.exe,这样就可以在Windows命令行运行Linux命令,比如通过scp把Windows下的文件拷贝到Linux下. 直接运行安装,不会生成任何客户端,直接使用W ...
- git 本地分支和远程分支改名字
1.将本地分支进行改名: git branch -m old_branch new_branch 2.将本地分支的远程分支删除: git push origin :old_branch 3.将改名后的 ...
- message box
QMessageBox 弹出框上的按钮设置为中文 Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::War ...
- 一台服务器绑定多个ip地址
一台服务器绑定多个ip地址. 方法一: 使用标准的网络配置工具(比如ifconfig和route命令)添加lP别名: 在eth0网卡再绑定一个ip:192.168.101.103 /sbin/ifco ...
- Netty权威指南(笔记一)
转载:http://blog.csdn.net/clarkkentyang/article/details/52529785 第一章(略) 第二章 NIO入门 2.1传统的BIO编程(同步阻塞I/O服 ...