leetcode02-Add Two Numbers之beats98.68%Java版本号
我的leetcode之旅,该篇章主要完毕使用Java实现算法。
这是第二篇Add Two Numbers
所有代码下载:
Github链接:github链接,点击惊喜;
写文章不易。欢迎大家採我的文章。以及给出实用的评论,当然大家也能够关注一下我的github。多谢。
1.题目简单介绍:英文。翻译找有道
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
2.我的思路:
1.题目须要做的是将两个链表进行按位求和,并进位。
2.我的解决的方法,将求和的值存放到来l2链表。
3.当l2到了null的时候,须要将l2的next指向l1(l1不为空).
4.须要注意的是当l1和l2都变空的时候假设进位为1。须要新建节点。
3.我的代码
/**
*
* @author peace
思路:题目须要做的是将两个链表进行按位求和,并进位。
我的解决的方法,将求和的值存放到来
l2链表。
当l2到了null的时候。须要将l2的next指向l1.
须要注意的是当l1和l2都变空的时候假设进位为1,须要新建节点。
*/
public class AddTwoNum {
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//边界判定
if(l1==null)return l1;
if(l2==null)return l2;
int k=0;//进位
int sum=0;//和
ListNode temp=l2;
ListNode head=l2;//使用l2存储求和后的值
while(l2!=null){
temp=l2;
if(l1!=null)//l1不为空时两者相加
{
sum=l1.val+l2.val+k;
l1=l1.next;
}else{//l1为空时候。仅仅对l2求和
if(k==0)break;
else{
sum=l2.val+k;
}
}
//对进位推断
if(sum>=10){
k=1;
sum=sum-10;
}else{
k=0;
}
//求和后的值存入链表
temp.val=sum;
l2=l2.next;
}
if(k!=0)//跳出l2的循环后推断k是否为0
{
temp.next=l1;
while(l2==null&&l1!=null){//当l1不为空时对l1进行求解
temp=l1;
sum=l1.val+k;
if(sum>=10){
k=1;
sum=sum-10;
}else{
k=0;
}
temp.val=sum;
l1=l1.next;
}
if(k!=0)//l1和l2都为空,新建节点
{
ListNode node=new ListNode(k);
temp.next=node;
}
}else if(l1!=null){//进位为空且l1不为空时直接连接到l1
temp.next=l1;
}
return head;
}
}
我的accept结果:
4.使用过的低效代码:
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ?
q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}
好的本章介绍到这里
来自伊豚wpeace(rlovep.com)
leetcode02-Add Two Numbers之beats98.68%Java版本号的更多相关文章
- 167. Add Two Numbers【LintCode by java】
Description You have two numbers represented by a linked list, where each node contains a single dig ...
- [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 两个数字相加
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(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- 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:基本加法规则 根据小学学的基本加法 ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
随机推荐
- hibernate 批量抓取
使用场景: 是查询出来一个集合,然后又查询每个集合对象中的集合.使用set标签中的batch-size属性实现. 数据库中只有5个区道信息: 设置batch-size=”5”,执行的查询语句如下: 而 ...
- WPF知识点--自定义Button(ControlTemplate控件模板)
ControlTemplate是一种控件模板,可以通过它自定义一个模板来替换掉控件的默认模板以便打造个性化的控件. ControlTemplate包含两个重要的属性:VisualTree 该模板的视觉 ...
- lua 之 三木运算符
在c语言中我三目运算符这么写: a?b:c 例如: max = a>b?a:b; 在lua中我们这么写 max = a>b and a or b 运行如下:
- ubuntu 12.04 配置iscsi共享及挂载iscsi共享
一.配置ubuntu 下iscsi下的target 1.配置iscsi-target: sudo apt-get install iscsi* 2.配置一个简单的iscsi target: iscsi ...
- tensorflow ConfigProto
tf.ConfigProto一般用在创建session的时候.用来对session进行参数配置 with tf.Session(config = tf.ConfigProto(...),...)#tf ...
- Quartz任务调度2
注意: 不同的版本的jar包,具体的操作不太相同,但是思路是相同的:比如1.8.6jar包中,JobDetail是个类,直接通过构造方法与Job类关联.SimpleTrigger和CornTrigge ...
- poj3134 Power Calculus
题目描述: 你现在有x^1,每动一步可以用当前存在的x^a和x^b获得x^(a+b)或x^(abs(a-b)).给出n(n<=1000),求最少多少步能得到x^n. 题解: IDDFS.枚举步数 ...
- Layui表格之动态添加数据(表格多选解决方案)
前言: Layui已经给出了多选记录的解决方案,是在每条数据的前面加上CheckBox,每次选择都有监听.效果是这样: 实现监听的代码如下,这是一种解决选择多条数据的方案: table.on('edi ...
- php 日常问题
1.isset.empty.is_null的区别 isset 判断变量是否定义或者是否为空 变量存在返回ture,否则返回false 变量定义不赋值返回false unset一个变量,返回false ...
- Python使用Flask框架,结合Highchart,自定义导出菜单项目及顺序
参考链接: https://www.highcharts.com.cn/docs/export-module-overview https://api.hcharts.cn/highcharts#ex ...