leetcode 2. Add Two Numbers [java]
注意点:
- 最后的进位
- (l1 == null || l1.next == null)
- break;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
boolean j = false;
ListNode p = new ListNode(0);
ListNode res = p;
while(true){
int a = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + (j ? 1 : 0);
if(a >= 10){
j = true;
p.val = a - 10;
}else{
j = false;
p.val = a;
}
l1 = (l1 == null || l1.next == null) ? null : l1.next;
l2 = (l2 == null || l2.next == null) ? null : l2.next;
boolean ok = l1 == null && l2 == null;
if(! ok){
p.next = new ListNode(0);
p = p.next;
}else
break;
}
if(j)
p.next = new ListNode(1);
return res;
}
leetcode 2. Add Two Numbers [java]的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
随机推荐
- ASP.NET MVC下载excel文档
问题来自论坛: 很早以前,学习做asp.net练习时,就是分享过<ASP.NET MVC应用程序实现下载功能>http://www.cnblogs.com/insus/p/3615714. ...
- 乐字节-Java8新特性之函数式接口
上一篇小乐带大家学过 Java8新特性-Lambda表达式,那什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口 ...
- Java的接口和抽象类
对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的时候会以为它们可以随意互换使用, ...
- K8s-Demo实现
Kubernates的基础界面 常用的操作 将创建好的yaml文件通过Create按钮创建所需资源项目. Dashbord: 可以通过Dashbord查看集群详情:cpu.memory.f ...
- apache伪静态原理图
- POJ2318(KB13-A 计算几何)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16222 Accepted: 7779 Description ...
- python学习之老男孩python全栈第九期_day004作业
看代码写结果:1. a=[1,2,3,6,"dfs",100]s=a[-1:]print (s) 结果:[100] 2. s=a[-1:0:-1]print(s) 结果:[100, ...
- python学习之老男孩python全栈第九期_day003知识点总结
1. int: i.bit_length(): 把i这个数转换成二进制所需最少的位数 2. bool: (1) 1比True的效率更高 (2) 空字符串(什么都没有)为False:非空字符串为True ...
- a 链接点击下载
1. 将链接设置为.zip 结尾2.在a元素中添加download 属性,(目前只有chrome.firefox和opera支持) function download(src) { var $a = ...
- javascript 数组求交集/差集/并集/过滤重复
最近在小一个小程序项目,突然发现 javscript 对数组支持不是很好,连这些基本的功能,都还要自己封装.网上查了下,再结合自己的想法,封装了一下,代码如下. //数组交集 Array.protot ...