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 ...
随机推荐
- Python开发——【条件】语句
单分支 # if 条件: # 满足条件后要执行的代码 if 2>1: print("2>1") 双分支 # if 条件: # 满足条件后要执行的代码 # else: # ...
- NC nc开发工具java虚拟机参数
-Dnc.exclude.modules=${FIELD_EX_MODULES} -Dnc.runMode=develop -Dnc.server.location=${FIELD_NC_HO ...
- finereport 下拉复选框多选
- 学习Tensorflow的LSTM的RNN例子
学习Tensorflow的LSTM的RNN例子 基于TensorFlow一次简单的RNN实现 极客学院-递归神经网络 如何使用TensorFlow构建.训练和改进循环神经网络
- python之路(六)-函数相关
在没有学习函数之前我们的程序是面向过程的,不停的判断,不停的循环,同样的代码重复出现在我们的代码里.函数可以更好的提高我们的 代码质量,避免同样的代码重复出现,而只需要在用的时候调用函数即可执行.此为 ...
- golang注意问题
关于slice 我们都知道slice是在通过参数传递的时候传递的是引用 slice的appen操作是有返回值的,并不改变原值 例如 b := [],,,} c:=append(b, ) // b 不变 ...
- 多态&虚函数
(1).对象类型: a.静态类型:对象声明时的类型,编译的时候确定 b.动态类型:对象的类型是运行时才能确定的 class A {}; class B:pub ...
- less的功能和介绍
在全新的css中,经过程序员们的开发和努力.又打造了全新的less样式表的全新界面,只需引入外部样式表即刻套用,加上了函数定义和取值.大大的降低了代码的书写量.也更加方便程序员的调用和修改!
- 请教:WCF速度似乎比Remoting慢
两段极为相似的代码,主要想看看过输与序列化过程两者的用时差异,结果10000次的调用,WCF用了11秒多,remoting用了5秒不到!这是测试的源代码 Remoting的服务端 public cla ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...