leetcode_445. Add Two Numbers II 单链表表示的两个大数相加
2017-01-05 20:51
323 查看
题目:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
题意:
两个单链表表示的大数,单链表的第一位为大数的最高有效位,求两个大数相加的和,结果也用单链表表示。假设两个大数没有先导0。
代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int [] arr1 = new int[100000]; //将第一个单链表存放在数组1中
int [] arr2 = new int[100000]; //将第二个单链表存放在数组2中
int len1 = 0;
int len2 = 0;
int i=0,j=0,k=0;
ListNode p = l1; //定义单链表结点
while( p != null ){ //存储第一个单链表
arr1[i] = p.val ;
i++;
p = p.next;
}
len1 = i;
p = l2;
while( p != null){ //存储第二个单链表
arr2[j] = p.val ;
j++;
p = p.next;
}
len2 = j;
i = len1-1; j=len2-1;
int res[] = new int[Math.max(len1,len2)+1]; //记录两个单链表的和
while(i >= 0 && j >= 0){
res[k] += arr1[i]+arr2[j];
if(res[k]>=10){
res[k+1] = res[k]/10;
res[k] = res[k]%10;
}
k++;
i--;
j--;
}
while(i>=0){ //计算剩余的单链表
res[k] += arr1[i];
if(res[k]>=10){
res[k+1] = res[k]/10;
res[k] = res[k]%10;
}
k++;
i--;
}
while(j>=0){ //计算剩余的单链表
res[k] += arr2[j];
if(res[k]>=10){
res[k+1] = res[k]/10;
res[k] = res[k]%10;
}
k++;
j--;
}
if(res[k] == 0){ //判断最高位是否有进位
k--;
}
ListNode head = new ListNode(0); //定义返回结果的头指针,逆序遍历数组res
if( k>=0 ){
p = new ListNode(res[k]);
head = p;
k--;
}
while(k>=0){ //逆序遍历数组res,将数组用单链表表示
ListNode q = new ListNode(res[k]);
p.next = q;
p = q;
k--;
}
return head;
}
}
笔记:
表示时间很长。
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7
题意:
两个单链表表示的大数,单链表的第一位为大数的最高有效位,求两个大数相加的和,结果也用单链表表示。假设两个大数没有先导0。
代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int [] arr1 = new int[100000]; //将第一个单链表存放在数组1中
int [] arr2 = new int[100000]; //将第二个单链表存放在数组2中
int len1 = 0;
int len2 = 0;
int i=0,j=0,k=0;
ListNode p = l1; //定义单链表结点
while( p != null ){ //存储第一个单链表
arr1[i] = p.val ;
i++;
p = p.next;
}
len1 = i;
p = l2;
while( p != null){ //存储第二个单链表
arr2[j] = p.val ;
j++;
p = p.next;
}
len2 = j;
i = len1-1; j=len2-1;
int res[] = new int[Math.max(len1,len2)+1]; //记录两个单链表的和
while(i >= 0 && j >= 0){
res[k] += arr1[i]+arr2[j];
if(res[k]>=10){
res[k+1] = res[k]/10;
res[k] = res[k]%10;
}
k++;
i--;
j--;
}
while(i>=0){ //计算剩余的单链表
res[k] += arr1[i];
if(res[k]>=10){
res[k+1] = res[k]/10;
res[k] = res[k]%10;
}
k++;
i--;
}
while(j>=0){ //计算剩余的单链表
res[k] += arr2[j];
if(res[k]>=10){
res[k+1] = res[k]/10;
res[k] = res[k]%10;
}
k++;
j--;
}
if(res[k] == 0){ //判断最高位是否有进位
k--;
}
ListNode head = new ListNode(0); //定义返回结果的头指针,逆序遍历数组res
if( k>=0 ){
p = new ListNode(res[k]);
head = p;
k--;
}
while(k>=0){ //逆序遍历数组res,将数组用单链表表示
ListNode q = new ListNode(res[k]);
p.next = q;
p = q;
k--;
}
return head;
}
}
笔记:
表示时间很长。
相关文章推荐
- 每天一道LeetCode-----将用数组表示的整数加一,两个用字符串表示的(二进制)数相加
- Leetcode Add Two Numbers 两个链表表示的数相加
- leetcode_415(两个数字字符串相加,模拟大数相加)
- leetcode_415. Add Strings 字符串表示的大数相加
- leetCode 之 两个二进制数相加
- LeetCode425——Add Strings(两个字符串中的数字相加(十进制或二进制),输出字符串形式的结果)
- 大数相加,两个大数。
- java实现两个大数相加,可能出现溢出错误
- 两个大数相加 ----Javascrit 实现
- 两个整型大数相加
- [LeetCode] 2. Add Two Numbers 两个数字相加
- C++实现的一个简单两个大数相加程序!
- 两个大数相加,使用字符串模拟相加过程
- 【C++】两个大数相加之HuaWei上机
- [Leetcode #2]Add Two Numbers 链表存储的两个正数相加
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
- 两个大数相加
- 稀疏矩阵的三元组表示的实现及应用(2)——采用三元组存储稀疏矩阵,设计两个稀疏矩阵相加的运算算法
- LeetCode | Add Two Numbers(两个链表相加)
- [Leetcode #2]Add Two Numbers 链表存储的两个正数相加