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 * ...
随机推荐
- Flask (一) 简介
Flask简介 Flask是一个基于Python实现的Web开发‘微’框架 'MicroFramework' Django是一个重型框架 官方文档: http://flask.pocoo.org/do ...
- idea获取激活码
访问地址拿到激活码:http://idea.lanyus.com/getkey
- 剑指tomcat之多项目部署问题
部署项目时遇到的问题,tomcat的webapps文件夹中有两个war包,但每次启动Tomcat服务时,只会默认启动一个war包. 解决方案一:在Tomcat主页中进入应用管理页面,手动开启项目.(进 ...
- SQL Server 2008添加字段成功,但提示列名无效
在sql后查询,给现有表添加一个字段,即执行sql语句: alter table [sxrq_1108].[dbo].[公司周报表详情] add 总计 int default 0 然后在上述sql查语 ...
- AzureARM 使用 powershell 扩容系统磁盘大小
azure中的虚拟机,windows磁盘大小为127G,linux磁盘大小为30G,在很多时候部署应用程序时直接部署到系统磁盘内导致磁盘后期容量不够需要扩容,在执行分区扩容前我们需要先通过Potal或 ...
- 洛谷 P2353 背单词
题目背景 小明对英语一窍不通,令老师十分头疼.于是期末考试前夕,小明被逼着开始背单词…… 题目描述 老师给了小明一篇长度为N的英语文章,然后让小明背M个单词.为了确保小明不会在背单词时睡着,老师会向他 ...
- 动手使用ABAP Channel开发一些小工具,提升日常工作效率
今天的故事要从ABAP小游戏说起. 中国的ABAP从业者们手头或多或少都搜集了一些ABAP小游戏,比如下面这些. 消灭星星: 扫雷: 来自我的朋友刘梦,公众号"SAP干货铺"里的俄 ...
- 解决activeandroid no such table
场景:activeandroid拷贝数据库 (1)复制sql数据库到项目的assets目录,例如/myapp/src/main/assets/prepop.db (2)确保manifest的AA_DB ...
- Fedora19添加和设置YUM源
Fedora19添加和设置YUM源添加yum源前先安装fastestmirror/downloadonly插件和axelget插件: 1.安装fastestmirror/downloadonly插件 ...
- Unity3d中MonoBehavior默认函数的执行顺序和生命周期
Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数 ...