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 ...
随机推荐
- 【SpringBoot系列5】SpringBoot整合RabbitMQ
前言: 因为项目需要用到RabbitMQ,前几天就看了看RabbitMQ的知识,记录下SpringBoot整合RabbitMQ的过程. 给出两个网址: RabbitMQ官方教程:http://www. ...
- [转]How to speed up Magento 2. Maximum Performance
本文转自:https://magedirect.co/how-to-speed-up-magento-2-maximum-performance/ Introduction In this artic ...
- ASP.NET MVC显示UserControl控件(扩展篇)
昨晚Insus.NET有怀旧一下<念念不忘,ASP.NET MVC显示WebForm网页或UserControl控件>http://www.cnblogs.com/insus/p/3641 ...
- 自动加载的iframe高度自适应
动态产生iframe,自动加载至body中,还有一个功能就是iframe的高度自适应,下面代码测试于IE和Firefox,Chrome:
- [Python] 函数基本
使用def 函数名(): 来定义一个函数,函数体一缩进块的形式写,返回结果是return xx 例如: def myAbs(x): if x >= 0: return x else: retur ...
- 解决VS2013 git客户端遇到的一些问题
分支问题 我在网上的托管环境(github)新建项目的时候有生成readme文件.在本地初始化项目添加git作为项目管理时,填写远程地址一定要以https形式,然后发布会出错,提示 在git命令行好像 ...
- javascript如何获取URL参数的值
function getUrlParameter(strParame){ var args = new Object( ); var query = location.search.substring ...
- django—xadmin中集成富文本编辑器ueditor
一.安装 pip命令安装,由于ueditor为百度开发的一款富文本编辑框,现已停止维护,如果解释器为python2,则直接pip install djangoueditor 解压包安装,python3 ...
- 微信小程序 折叠效果
<view class='help'> <view class='help_item'> <view class='title' data-index='1' catch ...
- NOI.AC NOIP2018 全国热身赛 第四场
心路历程 预计得分:\(0 + 100 +100\) 实际得分:\(10 + 100 + 0\) 神TM T3模数为啥是\(1e9 + 9\)啊啊啊啊,而且我也确实是眼瞎...真是血的教训啊.. T2 ...