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 ...
随机推荐
- SpringMVC之REST
REST: 即 Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网 ...
- Linux下安装SQL Server 2016(准备篇SQL Server on linux)
要在Linux安装sql server,首先的先有linux,下面会讲下载,然后安装Linux. 一.下载centos 7.2 : Centos 7.2下载 大概4G左右,这个链接的下载速度还是非常快 ...
- Docker 为 ASP.NET Core WebApi 应用程序生成 Docker 映像,创建容器并运行
1.使用VS2017新建ASP.NET Core WebApi项目 选择API启用Docker支持 2.为 ASP.NET Core WebApi 应用程序生成 Docker 映像,并创建容器运行 生 ...
- 深入理解Java虚拟机--阅读笔记一
Java内存区域 一.java运行时数据区域 1. 程序计数器:程序计数器占据的内存空间较小,是当前运行线程执行的字节码的计数:分支.循环.跳转.异常处理.线程恢复等都要依赖技术器来对执行的字节码进行 ...
- html5+css3图片旋转特效
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...
- git push报错error: failed to push some refs to 'git@github.com'
git push报错error: failed to push some refs to 'git@github.com' $ git push -u origin master To git@git ...
- Bootstrap(一)
Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. 使用感觉简约大气便捷,扁平化设计.好多好多CSS.JS....总有你心仪的那一款 ...
- input框中自动展示当前日期 yyyy/mm/dd
直接上代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- TNS-12549问题分析及解决办法
该服务器启动监听时候报错因为最后一句是Linux Error:No space left on device 因为是LINUX Error,所以可以到/var/log/messages里查看具体报错信 ...
- Android--listView的divider分割线样式和边距
1.建立一个drawable文件list_divider.xml <?xml version="1.0" encoding="utf-8"?> &l ...