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 * ...
随机推荐
- select查询---sql
SELECT 语句用于从数据库中选取数据. SQL SELECT 语句 SELECT 语句用于从数据库中选取数据. 结果被存储在一个结果表中,称为结果集. SQL SELECT 语法 SELECT c ...
- HDU3853(期望)
题目很水了,但是原地打转的点……虽然不难想到这个坑,但是从数学的角度来讲期望不应该算正无穷嘛……为什么算0啊? ; ; int R, C; struct gird { db ori, right, d ...
- Hive_Hive的数据模型_内部表
Hive的数据模型_内部表 - 与数据库中的Table在概念上是类似.- 每一个Table在Hive中都有一个相应的目录存储数据.- 所有的Table数据(不包括External Table)都保存在 ...
- 用javascript编写一个简单的随机验证码程序
简单模拟网页的随机数字验证码,效果图如下: html代码: <div id="content"> <div class="left"> ...
- mysql写存储过程并定时调用
设置一个定时任务:运行以下SQL -- 创建一个表test:字段endtime CREATE TABLE test (endtime DATETIME); -- 创建函数 :向test插入endt ...
- 一些JS基本小内容
获取select选中内容: 1.获取select表单内容 <select id="sel"> <option value="v1">1& ...
- uvm_reg_field——寄存器模型(二)
uvm_reg_field是最基本寄存器单元. typedef class uvm_reg_cbs; //----------------------------------------------- ...
- Azure资源管理工具Azure PowerShell介绍
什么是 Azure PowerShell? Azure PowerShell 是一组模块,提供用于通过 Windows PowerShell 管理 Azure 的 cmdlet.你可以使用 cmdle ...
- 新萝卜家园GHOST WIN7系统3专业装机版
系统来自系统妈:http://www.xitongma.com/ 系统概述 萝卜家园GHOST win7 64位装机旗舰版加快“网上邻居”共享速度:取消不需要的网络服务组件,系统支持Windows安装 ...
- P2421 A-B数对(增强版)
题目背景 woshiren在洛谷刷题,感觉第一题:求两数的和(A+B Problem)太无聊了,于是增加了一题:A-B Problem,难倒了一群小朋友,哈哈. 题目描述 给出N 个从小到大排好序的整 ...