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) + ...
随机推荐
- 迅为i.MX6Q嵌入式开发板
工业级核心板:核心板10层高速PCB设计,充分保证电磁兼容. 01. 处理器:开发板默认是四核商业扩展级芯片,可根据用户需求更换单核.双核.工业级.汽车级处理器,批量更省成本. 02. 扩展引脚:32 ...
- bzero - 向字符串写入零
总览 (SYNOPSIS) #include <string.h> void bzero(void *s, size_t n); 描述 (DESCRIPTION) bzero() 函数 把 ...
- vsphere中的linux虚拟机安装vmware-tools
先在vcenter中选中虚拟机点击安装这个工具,如图 然后这台linux虚拟机的控制台操作,挂载先建立挂载目录 cd /mnt #在挂载建一个用来挂载的文件. mkdir cdrom 使用mount命 ...
- CAD参数绘制圆弧(com接口)
在CAD设计时,需要绘制圆弧,用户可以在图面点圆弧起点,圆弧上的一点和圆弧的终点,这样就绘制出圆弧. 主要用到函数说明: _DMxDrawX::DrawArc2 由圆弧上的三点绘制一个圆弧.详细说明如 ...
- Delphi新注释
标准请看帮助文件里的:XML Documentation Comments 个人常用 <summary></summary>:摘要 /// <summary> // ...
- 无插件纯Web 3D机房,HTML5+WebGL倾力打造
前言 最近项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,以前一直搞java,对js的一些语言特性和概念一时还转换不过来. 上一篇大数 ...
- cc.Node—坐标系统
cc.Vec21: cc.Vec2 二维向量坐标, 表结构{x: 120, y: 120}; cc.v2(x, y) 创建一个二维向量 cc.p() 创建一个二外向量;2: cc.pSub: 向量相减 ...
- 51nod 1021 石子归并 - 区间dp(经典)
题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1021 经典区间dp,dp[i][j] 表示将从 i 到 j 堆 ...
- 小甲鱼python疑难点
1.python生成器 2.while 1: num = input('请输入一个整数(输入Q结束程序):') if num != 'Q': num = int(num) print('十进制 -&g ...
- Linux一键安装web环境全攻略phpstudy版
此教程主要是应对阿里云Linux云服务器ecs的web环境安装,理论上不限于阿里云服务器,此教程对所有Linux云服务器都具有参考价值. 写这篇文章的目的:网上有很多关于Linux一键安装web环境全 ...