LeetCode(2)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
分析:
AC代码:
/**
* 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)
{
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1; vector<int> v1;
vector<int> v2;
ListNode *head=NULL , *rear=NULL;
while(l1 != NULL)
{
v1.push_back(l1->val);
l1 = l1->next;
}
while(l2 != NULL)
{
v2.push_back(l2->val);
l2 = l2->next;
} if(v1.size() < v2.size())
{
for(int k=v1.size() ; k<v2.size() ; k++)
v1.push_back(0);
}else
{
for(int k=v2.size() ; k<v1.size() ; k++)
v2.push_back(0);
}
int temp = 0;
int value = 0;
for(int j=0 ; j<v1.size() ; j++)
{
int sum = v1[j] + v2[j] + temp;
temp = sum / 10;
value = sum % 10;
ListNode *node = new ListNode(value);
if(head == NULL)
head = node;
if(rear == NULL)
rear = node;
else
{
rear->next = node;
rear = rear->next;
}
}
if(temp != 0 && rear!=NULL)
{
ListNode *node = new ListNode(temp);
rear->next = node;
}
return head;
}
};
测试Main函数:
int main()
{
ListNode *l1=NULL , *r1=NULL, *l2 = NULL , *r2=NULL , *result=NULL;
int arr1[3] = {2,4,3};
int arr2[3] = {5,6,4};
for(int i=0 ; i<3 ; i++)
{
ListNode *node1 = new ListNode(arr1[i]);
ListNode *node2 = new ListNode(arr2[i]);
if(l1 == NULL)
l1 = node1;
if(r1 == NULL)
r1 = node1;
else{
r1->next = node1;
r1 = r1->next;
}
if(l2 == NULL)
l2 = node2;
if(r2 == NULL)
r2 = node2;
else{
r2->next = node2;
r2 = r2->next;
}
}
Solution s;
result = s.addTwoNumbers(l1,l2);
for( ; result!=NULL ; result=result->next)
cout<<result->val<<"->";
cout<<endl;
system("pause");
return 0;
}
LeetCode(2)Add Two Numbers的更多相关文章
- LeetCode(68)-Compare Version Numbers
题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if versio ...
- LeetCode(258) Add Digits
题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one dig ...
- LeetCode(165) Compare Version Numbers
题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...
- LeetCode(67) Add Binary
题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- 牛客寒假5-I.炫酷镜子
链接:https://ac.nowcoder.com/acm/contest/331/I 题意: 小希拿到了一个镜子块,镜子块可以视为一个N x M的方格图,里面每个格子仅可能安装`\`或者`/`的镜 ...
- 详细说明phpmyadmin连接,管理多个mysql服务器
用phpmyadimn来连接管理多个数据库要修改配置文件,挺不爽的,并且连接远程数据库,速度不行.可以使用其他数据库管理工具,请参考,navicat 结合快捷键 非常好用,开源,好用mysql 管理工 ...
- 一次Socket通信联想到的Zookeeper源码实现
最近做一个短信平台,要接入短信网关平台,网关平台使用C,短信平台属于公司内部对外应用,使用Java开发,两边通信采用TCP/IP协议
- prerender-spa-plugin预处理vue项目实践
由于公司想要把商城做由之前的php和前端混合开发改版为前后端分离,所以拿现在手上的vue项目来实践一下 https://github.com/chrisvfritz/prerender-spa-plu ...
- SpringBoot学习笔记-Chapter2(hello word)
开篇 第一次在博客园上写博客,初衷是想记录一下学习笔记,以往都是用笔去记录下学习笔记,现在来看在效率.检索速度上以及可可复制性都不好.作为一名Java开发人员 不会Spring Boot一定会被鄙视的 ...
- 用户会话跟踪机制(session+cookie)
最近在优化之前给学校写的一个项目,发现了同一个浏览器(IE,Firefox)开多个选项卡的时候不能登录多个用户,后一个登录用户会把前一个用户给覆盖了,我的登录逻辑是把user对象存放到session中 ...
- 2017 清北学堂 Day 6终极考试报告
预计分数: 100+70+70 = 240 实际假分数 : 40+80+70= 190 in cena(好吧不得不承认这个分数,,,,,,=.=) 实际真分数 : 100+80+100 = 280 ...
- 《Head First HTML与CSS》项目实践中学到的东西
1.组织的重要性. 首先是要建立两个根文件夹,一个存上线页面的资源,一个存测试页面的资源.所有改动内容都在测试页面的文件夹中进行,在这个文件夹中进行测试.W3C语法检测后(HTML检测网站:https ...
- DRP项目
DRP(distribution resource planning)分销资源计划是管理企业的分销网络的系统,目的是使企业具有对订单和供货具有快速反应和持续补充库存的能力.解决了随着企业销售规模的逐渐 ...
- .Net Mvc 返回Json,动态生成EasyUI Tree
最近做一个项目,开始接触EasyUI,感觉很强大,很适合我这种对前台不是很感冒的人.在学习Tree的过程中,感觉网上的资料挺乱的,很多只是把EasyUI API 抄了一遍.现在把最近这段时间的学到的, ...