LeetCode算法题2:Add Two Numbers
来源:互联网 发布:linux系统添加用户组 编辑:程序博客网 时间:2024/06/04 19:05
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
public class LeeAddTwoNum{ public static void main(String[] args) { // TODO Auto-generated method stub ListNode s, r,s2,r2; int x= 0; LeeAddTwoNum latn = new LeeAddTwoNum(); ListNode Head1 = new ListNode(0); Scanner scanner = new Scanner(System.in); //链表首部,单独处理 r = Head1; x = scanner.nextInt(); r.val = x; //接收输入,以29结束 x = scanner.nextInt(); while (x != 29) { s = new ListNode(x); r.next = s; r = s; x = scanner.nextInt(); } System.out.println(); //第二个 ListNode Head2 = new ListNode(0); r2 = Head2; x = scanner.nextInt(); r2.val = x; x = scanner.nextInt(); while (x != 29) { s2 = new ListNode(x); r2.next = s2; r2 = s2; x = scanner.nextInt(); } ListNode Ln; Ln = Head1; while(Ln!=null) { System.out.print(Ln.val+"-->"); Ln = Ln.next; } System.out.println("LL"); Ln = Head2; while(Ln!=null) { System.out.print(Ln.val+"-->"); Ln = Ln.next; } System.out.print("end"); System.out.println(); latn.addTwoNumbers(Head1, Head2); } public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode r,s,Head; int flag=0,i1,i2,i; Head = new ListNode((l1.val+l2.val)%10); r=Head; flag=(l1.val+l2.val)/10; l1=l1.next; l2=l2.next; while((l1!=null)||(l2!=null)) { if(l1==null) { i1=0; } else { i1=l1.val; l1=l1.next; } ///////////////////////////// if(l2==null) { i2=0; } else { i2=l2.val; l2=l2.next; } i=(i1+i2+flag)%10; flag=(i1+i2+flag)/10; //////////////////// s = new ListNode(i); r.next = s; r=s; } if(flag!=0) { r.next = new ListNode(flag); } //打印链表 // while(Head!=null) // { // System.out.print(Head.val+"-->"); // Head = Head.next; // } // System.out.print("end"); return Head; } //可以处理多位数的情况 public ListNode addTwoNumbers1(ListNode l1, ListNode l2) { ListNode r,s,Head; long i1=0; long i2=0; //取得链表里的数据,转为整数 StringBuffer sb =new StringBuffer(); StringBuffer sb2 =new StringBuffer(); while(l1!=null) { sb.append(l1.val); l1=l1.next; } while(l2!=null) { sb2.append(l2.val); l2=l2.next; } i1=Long.valueOf(sb.reverse().toString()); i2=Long.valueOf(sb2.reverse().toString()); i1=i1+i2; System.out.println("add="+i1); StringBuffer res = new StringBuffer(String.valueOf(i1)).reverse(); System.out.println("rese="+res.toString()); System.out.println(); int i; Head = new ListNode(Integer.valueOf(String.valueOf(res.charAt(0)))); r=Head; for(i=1;i<res.length();i++) { s = new ListNode(Integer.valueOf(String.valueOf(res.charAt(i)))); r.next = s; r=s; } while(Head!=null) { System.out.print(Head.val+"-->"); Head = Head.next; } System.out.print("end"); return Head; } public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }}
这种做法的复杂度有点高,希望有更好的方案。
0 0
- LeetCode算法题2:Add Two Numbers
- leetcode算法训练#2 Add Two Numbers
- LeetCode算法--2、Add Two Numbers
- 【LeetCode】算法题2 Add Two Numbers的归纳
- leetcode第2题-Add Two Numbers
- Leetcode 第2题Add Two Numbers
- Leetcode 第 2 题(Add Two Numbers)
- 【LeetCode】第2题: Add Two Numbers
- LeetCode 2 - Add Two Numbers
- LeetCode 2: Add Two Numbers
- Leetcode: Add Two Numbers (2)
- [leetcode 2] Add Two Numbers
- [Leetcode] 2 - Add Two Numbers
- LeetCode 2:《Add Two Numbers》
- LeetCode | #2 Add Two Numbers
- LeetCode 2 Add Two Numbers
- [Leetcode]2Add Two Numbers
- leetcode #2 Add Two Numbers
- Fun House
- 持续更新--JSP网站建构中遇到的问题
- eclipse 快捷键
- 关于static在java和C++中的用法小谈(一)
- 经典算法题每日演练——第十题 树状数组
- LeetCode算法题2:Add Two Numbers
- (二)OGG双向DML复制操作
- sql语句简单手册
- 嵌入式常用板上通信接口
- 跳过Windows版本的Android Studio安装后首次启动
- yourphp二次开发目录
- 经典算法题每日演练——第十一题 Bitmap算法
- 显示转换static_cast、const_cast、reinterpret_cast、dynamic_cast详解
- Hadoop入门进阶课程8--Hive介绍和安装部署