我的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. [分享] IMX6嵌入式开发板linux QT挂载U盘及TF卡

    本文转自迅为开发板:http://www.topeetboard.com 开发平台:iMX6开发板 linux QT 系统下挂载 u 盘如下图所示,qt 启动之后,在超级终端中使用命令“mknod / ...

  2. 异步编程when.js

    when.js很小,压缩后只有数kb,gzip后的大小几乎可以忽略.在Node和浏览器环境里都可以使用when.js 首先,我们看一小段代码: var getData = function(callb ...

  3. vue工程化之项目引入jquery

    既然写项目,那么少不了用jq,那我们就引入进来吧 1.因为已经安装了vue脚手架,所以需要在webpack中全局引入jquery 打开package.json文件,在里面加入这行代码,jquery后面 ...

  4. 允许IIS下载无后缀文件及“请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。”的解决方法

    1)增加MIME类型 ,如下 application/octet-stream 2)注意是"." , ".*"则适用于任何有文件后缀时使用,无后缀请不要加*

  5. String 工具类

    package com.mytripod.util; import sun.rmi.runtime.Log; import java.io.UnsupportedEncodingException; ...

  6. 微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)

    1.wxml代码 <!--index.wxml--> <view class="container"> <button type='primary' ...

  7. Windows系列原版系统镜像下载

    原版系统镜像下载 Windows 10 系统 Windows 10 企业版 1511版 (64位) Windows 10 Enterprise, Version 1511 (x64) – DVD (C ...

  8. Python之爬虫-段子网

    Python之爬虫-段子网 https://ishuo.cn #!/usr/bin/env python # -*- coding:utf-8 -*- import re import request ...

  9. NormalMap 法线贴图

    法线贴图+纹理贴图(细节明显) 纹理贴图 法线贴图 法线贴图 存储法线的一张贴图,归一化的法线的 xyz 的值被映射成为对应的 RGB 值.归一化的法线值为[-1,1],RGB的每一个分量为无符号的8 ...

  10. HTML文件中css样式的引用

    1.1.直接在div中使用css样式制作div+css网页 如: <div style="font-size:14px; color:#FF0000;">内容</ ...