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 ...
随机推荐
- zoj 2744 Palindromes(计算回文子串个数的优化策略)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2744 题目描述: A regular palindrome i ...
- JVM 综述
概览 从 JVM 的总体上看,它解决了3个问题: Java 程序的内存管理(GC & 运行时数据区). Java Class 二进制字节流的加载(ClassLoader). Java 程序的执 ...
- [转]RSReportServer 配置文件
本文转自:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2005/ms157273(v=sql.90) 更新日期: ...
- 在ASP.NET MVC使用JavaScriptResult
本例中,我们尝试把javascript程序搬至控制器中去.更好地在控制器编写程序. 首先来看看原来的写法. 在SepController控制器,添加如下操作: public ActionResult ...
- Git-分支创建、拉取、切换
git新建本地分支命令 1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义. 2.创建远 ...
- 在VS2010中使用Git管理源代码
前文我们讲了使用TortoiseGit管理源代码,但是对于使用VS2010的朋友来说,源代码管理起来还是不怎么方便.要是直接在VS2010中能使用Git就好了,下面我们就来看看怎么在VS2010中使用 ...
- 2017.12.21-JQuery
作业:密码加强验证 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- oracle创建表的方法和一些常用命令
1.主键和外键主键:关系型数据库中的一条记录中有若干个属性,若其中的某一个属性组(注意是组,可以是一个,也可以是多个)能唯一标识一条记录,那么该属性组就是主键外键:关系型数据库表中的一列或者某几列的组 ...
- Android - Navigation Drawer
http://www.jianshu.com/p/c8cbeb7ea43a 用Navigation Drawer 和 Navigation View 来实现左右侧滑 Activity里甚至什么都不用写 ...
- python基础训练题1-列表操作
1,在列表末尾添加一个值 >>> l = [ 10, 20 ] >>> l [10, 20] >>> l.append( 'ghostwu' ) ...