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. C#日志

    参考页面: http://www.yuanjiaocheng.net/Entity/first.html http://www.yuanjiaocheng.net/Entity/jieshao.htm ...

  2. IdentityServer4 使用OpenID Connect添加用户身份验证

    使用IdentityServer4 实现OpenID Connect服务端,添加用户身份验证.客户端调用,实现授权. IdentityServer4 目前已更新至1.0 版,在之前的文章中有所介绍.I ...

  3. 【从零开始学BPM,Day4】业务集成

    [课程主题] 主题:5天,一起从零开始学习BPM [课程形式] 1.为期5天的短任务学习 2.每天观看一个视频,视频学习时间自由安排. [第四天课程] 1.课程概要 Step 1 软件下载:H3 BP ...

  4. .NET跨平台之旅:数据库连接字符串写法引发的问题

    最近在一个ASP.NET Core站点中遇到一个奇怪问题.当用dotnet run命令启动站点后,开始的一段时间请求执行速度超慢,有时要超过20秒,有时甚至超过1分钟,日志中会记录这样的错误: Sys ...

  5. shell之sort命令

    1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [rocrocket@rocrocket progr ...

  6. 【AI开发第一步】微软认知服务API应用

    目录 介绍 API分类 使用‘视觉’API完成的Demo 点击直接看干货 介绍 从3月份Google家的阿尔法狗打败韩国围棋冠军选手李世石,到之后微软Build2016大会宣布的“智能机器人”战略.种 ...

  7. 让ASP.NET接受有“潜在危险”的提交

    什么是有“潜在危险”的提交?马上动手写个简单的例子:   用Visual Studio创建一个空白的ASP.NET MVC程序,一切默认即可,添加一个空白的HomeController,增加一个Ind ...

  8. 虚拟机体验之 KVM 篇

    在上一篇中,我展示了虚拟机软件 QEMU 的使用.效果及其性能,同时也分析了不同用户对虚拟机的不同追求.但是不管是桌面用户还是企业级用户,对虚拟机软件的追求有一点是共同的,那就是性能.QEMU 是一个 ...

  9. ASP.net 内置对象

    .net初学者,有错误欢迎指正.大家共同进步 Response 输出数据 Reponse对象和Request对象组成了一对发送,接受数据的对象. 发送信息:Reponse.Write("字符 ...

  10. angular2系列教程(四)Attribute directives

    今天我们要讲的是ng2的Attribute directives.顾名思义,就是操作dom属性的指令.这算是指令的第二课了,因为上节课的components实质也是指令. 例子