leetcode — add-two-numbers
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的更多相关文章
- [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 ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- TUN/TAP编程实现
其实关于这两种设备的编程,基本上属于八股文,大家一般都这么干. 启动设备之前 有的linux 并没有将tun 模块编译到内核之中,所以,我们要做的第一件事情就是检查我们的系统是否支持 TUN/TAP ...
- 查看memcached进程
- Python(四) 列表元组
- oracle删除dbf导致的oracle工具不能正常使用
1.使用cmd命令登录Oracle:sqlplus / as sysdba;就可以,中间两个空格.2.删除了dbf导致Oracle工具不能正常使用解决办法(oracle initialization ...
- P1613 跑路(倍增 + floyd)
https://www.luogu.org/problemnew/show/P1613 思路: 1.读入 2.建图 3.对于每一个点,向距离它 2^k 长度的点连一条长度为 1 的边 4.在新图上跑1 ...
- TJOI2018 简要题解
数学计算 用线段树记录之前乘过的每一个数,作除法时把原本的乘数改成111即可. 代码: #include<bits/stdc++.h> #define lc (p<<1) #d ...
- drf4 视图与路由组件
APIView和View的区别 不管是View还是APIView最开始调用的都是as_view() APIView继承了View, 并且执行了View中的as_view()方法,最后把view返回了, ...
- 从中央仓库下载所想要的jar包
中央仓库地址:https://mvnrepository.com/ 这边我搜索一个commons-logging包作为例子: 点击下面第二个绿色的comons-logging进入这个页面: 一.win ...
- android-基础编程-ToolBar
Android 3.0 Android 推了 ActionBar 这个控件,而到了2013 年 (4.0)Google 开始大力地推动所谓的 android style,material desig ...
- 关于wifi网络基本原理了解
对于esp32,其wifi功能还是十分强大的,为了能够良好的完成wifi的相关开发,这里需要计算机网络的结构体系进行大致的了解. 一.网络结构分层 对于计算机网络结构,大体上可以分为5层结构: 物理层 ...