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. 来自于微信小程序的一封简讯

    9月21晚间,微信向部分公众号发出公众平台-微信应用号(小程序)的内测邀请,向来较为低调的微信在这一晚没人再忽视它了. 来自个人博客:Damonare的个人博客 一夜之间火了的微信应用号你真的知道吗? ...

  2. JQuery中的siblings()是什么意思

    jQuery siblings() 方法返回被选元素的所有同胞元素,并且可以使用可选参数来过滤对同胞元素的搜索. 实例演示:点击某个li标签后将其设置为红色,而其所有同胞元素去除红色样式. 1.创建H ...

  3. gRPC源码分析1-SSL/TLS

    引子 前几天看到微信后台团队分享了TLS相关文章,正好gRPC里TLS数据加密是很重要的一块,于是整理出了这篇文章. 在gRPC里,如果仅仅是用来做后端微服务,可以考虑不加密.本文太长,先给个大纲. ...

  4. 太多选择——企业如何选择合适的BI工具?

    在没认清现状前,企业当然不能一言不合就上BI. BI不同于一般的企业管理软件,不能简单归类为类似用于提高管理的ERP和WMS,或用于提高企业效率的OA.BPM.BI的本质应该是通过展现数据,用于加强企 ...

  5. (一)Spark简介-Java&Python版Spark

    Spark简介 视频教程: 1.优酷 2.YouTube 简介: Spark是加州大学伯克利分校AMP实验室,开发的通用内存并行计算框架.Spark在2013年6月进入Apache成为孵化项目,8个月 ...

  6. charles工具抓包教程(http跟https)

    1.下载charles 可以去charles官网下载,下载地址:http://www.charlesproxy.com/download/    根据自己的操作系统下载对应的版本,然后进行安装,然后打 ...

  7. 【腾讯Bugly干货分享】Android性能优化典范——第6季

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/580d91208d80e49771f0a07c 导语 这里是Android性能优 ...

  8. 【深入浅出jQuery】源码浅析2--奇技淫巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  9. ABP框架 - 数据过滤

    文档目录 本节内容: 简介 预定义过滤 ISoftDelete 何时可用? IMustHaveTenant 何时可用? IMayHaveTenant 何时可用? 禁用过滤 关于using声明 关于多租 ...

  10. Rxjava cold/hot Observable

    create Observable分为cold以及hot两种,cold主要是静态的,每次subscribe都是从头开始互不干扰,而hot的在同一时刻获得的值是一致的 cold Observable 使 ...