add-two-numbers
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的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [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 ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- Two Sum & Add Two Numbers
Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
随机推荐
- 马里奥AI实现方式探索 ——神经网络+增强学习
[TOC] 马里奥AI实现方式探索 --神经网络+增强学习 儿时我们都曾有过一个经典游戏的体验,就是马里奥(顶蘑菇^v^),这次里约奥运会闭幕式,日本作为2020年东京奥运会的东道主,安倍最后也已经典 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- Git的四个基本概念及 git的工作流程
- Log4Net应用问题
问题 一.日志存储方式 1.txt 2.SQLServer数据库 3.log文件 二.项目类型不同 1winFrom 2webFrom 3MVC 4WPF 5控制台 三.切分依据不同 1.空间大小 2 ...
- 【一起学OpenFoam】02 软件准备
"工欲善其事必先利其器",在利用OpenFoam解决我们的工程问题之前,首先要做的事情是搭建一个OpenFoam运行环境.很遗憾的是,OpenFoam的原生开发系统是Linux,因 ...
- NOIP2016纪录[那些我所追求的]
人生第一场正式OI [序] 2016-12-04 见底部 [Day -1] 2016-11-17 期中考试无心插柳柳成荫,考了全市第2班里第1(还不是因为只复习了不到两天考试),马上请了一个周的假准备 ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- 当前端也拥有 Server 的能力
今天看了不少文章,比较感兴趣的是 Cache API.它是浏览器 Request/Response 的缓存管理工具,其使用风格和运用场景让我瞬间联想到了 ServiceWorker 和 Fetch A ...
- CanvasWebgl项目介绍
CanvasWebgl 介绍 CanvasWebgl 是一个基于webgl 开发的2d绘图框架,使用TypeScript开发 CanvasWebgl的功能,是在屏幕空间 或者 3D空间产生一个画布 ...
- MSSQL 基础语句笔记
建库 CREATE DATABASE 数据库名 ON[PRIMARY] --默认属于PRIMARY主文件组,可省略 ( NAME='', --主数据文件的逻辑名 名称 FILEAME='', --主数 ...