import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/add-two-numbers/
*
* Created by lverpeng on 2017/6/23.
*
* 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 AddTwoNumbers { /**
* 问题就是求两个数的和,只不过这两个数分别存在两个链表中,低位在前,高位在后,求出的和也存在链表中
* 循环较长的那个链表:
* 如果是当前有个链表都有值,则将两个数与上次计算的商(也就是进位)求和,结果的对于10的余数存于结果链表对一个的位置,结果对于10的商保存在变量中(也就是进位),
* 如果已经超过了其中一个链表的长度,则直接把另一个链表的数存入结果链表
*
* @param num1
* @param num2
* @return
*/
public List<Integer> addTwoNumbers (List<Integer> num1, List<Integer> num2) {
List<Integer> numbers = new ArrayList<Integer>();
int size = 0;
if (num1.size() > num2.size()) {
size = num1.size();
} else {
size = num2.size();
} int carry = 0;
int sum = 0;
for (int i = 0; i < size; i++) {
if (num1.size() > i && num2.size() > i) {
sum = carry + num1.get(i) + num2.get(i);
} else if (num1.size() - 1 > i) {
sum = carry + num1.get(i);
} else {
sum = carry + num2.get(i);
}
numbers.add(sum % 10);
carry = sum / 10;
}
if (carry > 0) {
numbers.add(carry);
}
return numbers;
} public List<Integer> addTwoNumbersRefactor (List<Integer> num1, List<Integer> num2) {
List<Integer> result = new ArrayList<Integer>();
int size = num1.size() > num2.size() ? num1.size() : num2.size();
int carry = 0;
int sum = 0;
for (int i = 0; i < size; i++) {
sum = carry + getNumber(num1, i) + getNumber(num2, i);
carry = sum / 10;
result.add(sum % 10);
}
if (carry > 0) {
result.add(carry);
}
return result;
} private int getNumber(List<Integer> numbers, int i) {
if (i < 0 || i >= numbers.size()) {
return 0;
}
return numbers.get(i);
} public static void main(String[] args) {
AddTwoNumbers addTwoNumbers = new AddTwoNumbers();
List<Integer> num1 = new ArrayList<Integer>(){{
add(2);
add(4);
add(3);
}};
List<Integer> num2 = new ArrayList<Integer>(){{
add(5);
add(6);
add(4);
}};
System.out.println(addTwoNumbers.addTwoNumbers(num1, num2));
System.out.println(addTwoNumbers.addTwoNumbersRefactor(num1, num2));
}
}

leetcode — 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. LeetCode Add Two Numbers II

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

  4. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  5. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  6. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  7. [Leetcode] Add two numbers 两数之和

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

  8. [LeetCode] Add Two Numbers

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

  9. LeetCode——Add Two Numbers

    Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...

  10. [LeetCode] Add Two Numbers 链表

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

随机推荐

  1. Spring的介绍与搭建

    一.Spring的介绍 二.Spring的搭建 (1)导包 (2)创建一个对象 (3)书写配置注册对象到容器 (4)代码测试

  2. 使用Shell脚本对Linux系统和进程资源进行监控

    ShellLinux脚本 摘要:Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.本文我们以Bash做为实例总结了使用Shell对系统和进 ...

  3. pyhon_day1 格式化输出

    格式化输出 我们在写Python的在很多时候输出的内容需要规定格式的,这样就引入到了格式化输出 定义: 盗用下大咖的例子: ------------ info of alex ----------- ...

  4. Altera 在线资源使用

    Altera 在线资源使用 Altera 在线资源使用 1 1.Altera中文版 2 2.建立myaltera账户 获取官网信息与支持 2 3系统化的设计资源 2 3.1.设计实例 2 3.2.参考 ...

  5. Python 装饰器(笔记,非原创)

    定义:本质是函数,为其他函数添加附加功能原则:1.不能修改被装饰的函数的源代码         2.不能修改被装饰的函数的调用方式知识储备:       1.函数即“变量”       2.高阶函数  ...

  6. Error:(18, 51) java: -source 1.5 中不支持 diamond 运算符 (请使用 -source 7 或更高版本以启用 diamond 运算符)

    问题:主要是因为jdk版本不一样 解决: 方法一:List<String> list=new ArrayList<Stirng>(); 方法二:重新安装jdk8的版本(安装和配 ...

  7. zabbix教程

    zabbix官方文档:https://www.zabbix.com/documentation/current/zh/manual zabbix视频教程:https://www.bilibili.co ...

  8. Runtime之方法

    前两篇介绍了类与对象.成员变量&属性&关联对象的相关知识,本篇我们将开始讲解Runtime中最有意思的一部分内容:消息处理机制.我们从一个示例开始. 在OC中,我们使用下面这种方式来调 ...

  9. 不修改模板的前提下修改VisualState中的某些值

    原文链接:不修改模板的前提下修改VisualState中的某些值 - 超威蓝火 UWP里有一件非常令人不爽的事,大部分控件只提供了Normal状态下的Background,Foreground,Bor ...

  10. leetcode144-先序遍历非递归实现

    二叉树的先序/中序/后序遍历递归/非递归实现,讲的很清楚,其中后序遍历和先序中序的处理有些不一样: https://blog.yangx.site/2016/07/22/Python-binary-t ...