我的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. QList模板类常用接口函数

    插入操作:insert()函数原型:void QList::insert(int i, const T &value) 在索引后插入值 i:索引 value:插入值 Example: QLis ...

  2. WPS 常用操作

    1.WPS屏保太美了,如何保存 网上搜到如下资料,发现可以在电脑中找到若干个被缓存的图片,kwallpaper可能为kscreensaver

  3. ZXing.dll 生成二维码 C# winform net4.5

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. CAD参数绘制文字(com接口)

    在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...

  5. 使用nsight调试caffe

    首先你需要下载caffe源码,然后先编译好,注意一定要将Makefile.config里的DEBUG := 1注释掉 可以看到注释掉debug后编译会生成的.build_debug目录,调试过程中需要 ...

  6. 三个层面学playbook(核心)

    三个层面学playbook(核心) ansible-playbook是ansible工具中的核心,对比ad-hoc(ansible)命令,可以把playbook理解为一系列动作的组成,结果传递.判断等 ...

  7. 编译压缩代码 MFCompress-src-1.01 :对‘***’未定义的引用

    提示 MFCompressD.o:在函数‘main’中:MFCompressD.c:(.text.startup+0x34a): 警告: the use of `tempnam' is dangero ...

  8. find -print0和xargs -0原理及用法

    平常我们经常把find和xargs搭配使用,例如: find . -name "*.txt" | xargs rm 但是这个命令如果遇到文件名里有空格或者换行符,就会出错.因为xa ...

  9. RNNCell使用

    目录 Recap input dim, hidden dim SimpleRNNCell Single layer RNN Cell Multi-Layers RNN RNN Layer Recap ...

  10. 主席树模板poj 2104

    资料1:http://blog.csdn.net/regina8023/article/details/41910615 资料2:模板来源:http://www.cnblogs.com/lidaxin ...