leetcode开篇~

问题描述:

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

首先要看懂题目,囧。第一次看,着实没有看懂。。

翻译一下:用两个链表表示两个非负整数。链表每个节点表示一个数字,按照数字倒序排列。比如123,用链表表示是3->2->1。求两个数字相加的和,并用链表表示。如342+465=807,用链表表示为7->0->8.

解答:

第一次写完使用的是递归的方式,结果显示超时。如下:

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return addTwoNumbers(l1, l2, false);
} private static ListNode addTwoNumbers(ListNode l1, ListNode l2, boolean putMore){
if(l1 == null && l2 == null){
return putMore ? new ListNode(1) : null;
}else if(l1 == null){
return putMore ? addTwoNumbers(l2, new ListNode(1), false) : l2;
}else if(l2 == null){
return putMore ? addTwoNumbers(l1, new ListNode(1), false) : l1;
} int value = putMore ? (l1.val+l2.val+1) : (l1.val+l2.val);
putMore = value >= 10 ? true : false;
ListNode result = new ListNode(value%10);
result.next = addTwoNumbers(l1.next, l2.next, putMore);
return result;
}

 然后查看了其他人的解答,改为非递归方式:

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode node = head;
ListNode p1 = l1;
ListNode p2 = l2;
boolean putMore = false;
while (p1 != null || p2 != null || putMore){
int add1 = p1 == null ? 0 : p1.val;
int add2 = p2 == null ? 0 : p2.val;
int value = putMore ? (add1+add2+1) : (add1+add2);
putMore = value >= 10 ? true : false;
node.next = new ListNode(value%10);
if(p1 != null) p1 = p1.next;
if(p2 != null) p2 = p2.next;
node = node.next;
}
return head.next;
}

  

加上测试代码:

    private static ListNode initListNode(List<Integer> valueList){
ListNode result = new ListNode(valueList.get(0));
ListNode node = result;
for(int i=1; i<valueList.size(); i++){
node.next = new ListNode(valueList.get(i));
node = node.next;
}
return result;
} private static List<Integer> getResult(ListNode node){
List<Integer> result = Lists.newArrayList();
ListNode temp = node;
while (temp != null){
result.add(temp.val);
temp = temp.next;
}
return result;
} public static void main(String[] args) {
ListNode l1 = initListNode(Lists.newArrayList(6));
System.out.println("l1:" + JSON.toJSONString(getResult(l1)));
ListNode l2 = initListNode(Lists.newArrayList(6,9));
System.out.println("l2:" + JSON.toJSONString(getResult(l2)));
ListNode node = addTwoNumbers(l1, l2);
System.out.println("result:" + JSON.toJSONString(getResult(node)));
}

  

总结:

递归方式解答问题,比较容易想到。有了递归解答,要试着改成非递归的形式,提高性能。

add-two-numbers的更多相关文章

  1. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  2. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  4. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  5. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  6. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  9. Two Sum & Add Two Numbers

    Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...

  10. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

随机推荐

  1. 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇一:WPF常用知识以及本项目设计总结

    篇一:WPF常用知识以及本项目设计总结:http://www.cnblogs.com/baiboy/p/wpf.html 篇二:基于OneNote难点突破和批量识别:http://www.cnblog ...

  2. iOS 委托与文本输入(内容根据iOS编程编写)

    文本框(UITextField) 本章节继续编辑 JXHypnoNerd .文件地址 . 首先我们继续编辑  JXHypnosisViewController.m 修改  loadView 方法,向  ...

  3. Android 添加ActionBar Buttons

    一.在res/menu文件夹下创建Xml文件 跟标签为menu,设置item <?xml version="1.0" encoding="utf-8"?& ...

  4. (整理)MyBatis入门教程(一)

    本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...

  5. js中的null 和undefined

    参考链接:http://blog.csdn.net/qq_26676207/article/details/53100912 http://www.ruanyifeng.com/blog/2014/0 ...

  6. 从零开始,DIY一个jQuery(3)

    在前两章,为了方便调试,我们写了一个非常简单的 jQuery.fn.init 方法: jQuery.fn.init = function (selector, context, root) { if ...

  7. 【腾讯Bugly干货分享】动态链接库加载原理及HotFix方案介绍

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57bec216d81f2415515d3e9c 作者:陈昱全 引言 随着项目中动 ...

  8. .NET应用程序与数据库交互的若干问题

    我们知道,在应用程序中与数据库进行交互是一个比较耗时的过程,首先应用程序需要与应用程序建立连接,然后将请求发送到数据库,数据库执行操作,然后将结果集返回.所以在程序中,要尽量晚的与数据库建立连接,并且 ...

  9. CSharpGL(21)用鼠标拾取、拖拽VBO图元内的点、线或本身

    CSharpGL(21)用鼠标拾取.拖拽VBO图元内的点.线或本身 效果图 以最常见的三角形网格(用GL_TRIANGLES方式进行渲染)为例. 在拾取模式为GeometryType.Point时,你 ...

  10. Entity Framework 6 Recipes 2nd Edition(11-5)译 -> 从”模型定义”函数返回一个匿名类型

    11-5. 从”模型定义”函数返回一个匿名类型 问题 想创建一个返回一个匿名类型的”模型定义”函数 解决方案 假设已有游客(Visitor) 预订(reservation)房间(hotel ) 的模型 ...