我的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版本号的更多相关文章

  1. 167. Add Two Numbers【LintCode by java】

    Description You have two numbers represented by a linked list, where each node contains a single dig ...

  2. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  3. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  4. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  6. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  7. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  8. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  9. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

随机推荐

  1. Laravel Homestead的安装和使用(照搬)

    原文:https://blog.csdn.net/woqianduo/article/details/81091154/ 1.简介 1.1.Homestead是什么 Laravel Homestead ...

  2. uploadify插件可选参数的详细介绍

    Uploadify 是一个JQuery插件,它协助你轻松简单的将一个或多个文件上传至你的网站.  它需要Flash控件和后台开发语言的支持,丰富的参数配置,同时也简单易用,让你轻松上手.      官 ...

  3. shell learning note

      shell learning note MAIN="/usr/local/" # 变量大写 STATUS="$MAIN/status" # 美元符加字符串是 ...

  4. [PyTorch] rnn,lstm,gru中输入输出维度

    本文中的RNN泛指LSTM,GRU等等 CNN中和RNN中batchSize的默认位置是不同的. CNN中:batchsize的位置是position 0. RNN中:batchsize的位置是pos ...

  5. 2D热力图实例

    <div style="height: 100px; width: 200px" id="heatmap"></div> <scr ...

  6. 【简●解】POJ 1845 【Sumdiv】

    POJ 1845 [Sumdiv] [题目大意] 给定\(A\)和\(B\),求\(A^B\)的所有约数之和,对\(9901\)取模. (对于全部数据,\(0<= A <= B <= ...

  7. 用PHP的GD库画五星红旗来玩玩

    1 header("Content-Type:image/jpeg"); $img=imagecreatetruecolor(999,667); $color=imagecolor ...

  8. Vue页面骨架屏(二)

    实现思路 参考原文中在构建时使用 Vue 预渲染骨架屏一节介绍的思路,我将骨架屏也看成路由组件,在构建时使用 Vue 预渲染功能,将骨架屏组件的渲染结果 HTML 片段插入 HTML 页面模版的挂载点 ...

  9. 【04】 CSS开发注意事项

    [04] CSS注意事项 1. 页面编码规范 1.1. 统一使用 UTF-8 编码,用@charset "utf-8"指定页面编码. 1.2. 全局字体设置: windows 7系 ...

  10. Spring框架中 配置c3p0连接池

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...