【2017.11.29】2.Add Two Numbers

2024-03-18 23:59
文章标签 numbers 29 two add 2017.11

本文主要是介绍【2017.11.29】2.Add Two Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2.1 non-非

non- 无,非,不
ag:The countries in the region signed a non-aggression pact。在该地区的国家签订了一个互不侵犯的条约

non-empty :非空的
non-negatice:非负的

2.2 node:节点

表示链表中的结点。

c.pseudcode 伪代码

executable pseudcode 可执行的伪代码、

伪代码是一种算法描述语言。使用伪代码的算法可以同意地以任何一种变成语言(pascal,c,java)实现。
因此伪代码必须结构清晰,代码简单,可读性好,并且类似自然语言。

2.3 a linked list:链表

在计算机科学中,链表是数据元素的线性集合,其中线性顺序不是由他们在存储器中的物理位置给出的。相反,每一个元素指向下一个,节点组成的数据结构,这些节点一起表示一个序列。在最简单的形势下,每个节点由数据和引用组成。
( o=^•ェ•)o ┏━┓
你可以把链表想象成火车,节点就是其中一节节的车厢,通过通道和前后车厢相连。因为节点中要包含数据、只想前后车厢的链接,所以一般是复合型的结构体。

这里写图片描述
链表,其节点包含两个字段:整数值和到下一个节点的链接。最后一个节点链接到用于表示列表结尾的终止符。

2.4 java-dummy head 假表头

有了dummy head之后,所有的节点都变成了拥有前置节点的节点了,所以就不用担心处理头结点这个特殊的情况了。而且你最后需要返回的仅仅是dummy.next,不用发功夫保住你的头节点了。

2.5 Initialize初始化

2.6 set-node-value :设置节点的值

2.7 listNode 链表

//单链表数据结构
public ListNodeP{Object element;ListNode next;ListNode(Obeject o,ListNode n){element=o;next=n;}ListNode(Object o){this(o,null);}
}

2.Add Two Number

题目:

You are given two non-empty linked lists(非空链表) representing two non-negative integers(非负的整数). 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

解析:

Intution:(直观解释)

Keep track of the carry using a variable and simulate digits-by-digits sum starting from the head of list, which contains the least-significant digit.
这里写图片描述

Algorithm(算法):

The pseudocode(伪代码) is as following:

  • Initialize current node to dummy head(假表头) of the returning list.
  • Initialize carry to 0.
  • Initialize p and q to head of l1 and l2 respectively(分别地).
  • Loop through lists(通过列表循环) l1 and l2 until you reach both ends(知道两端).
    • Set x to node p’s value. If p has reached the end of l1, set to 0.
    • Set y to node q’s value. If q has reached the end of l2, set to 0.
    • Set sum = x + y + carry.
    • Update carry = sum / 10.
    • Create a new node with the digit value of (sum mod 10)and set it to current node’s next, then advance current node to next.
    • Advance both pp and qq.
  • Check if carry = 1, if so append a new node with digit 1 to the returning list.
  • Return dummy head’s next node.
    Note that we use a dummy head to simplify the code. Without a dummy head, you would have to write extra conditional statements to initialize the head’s value.
    我们实用虚拟头来简化代码,没有虚拟头,你不得不编写额外的条件语句来初始化头的值。

Take extra caution of the following cases:
这里写图片描述

code:

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) { val = x; }* }*/
class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {//初始化虚拟表头为0ListNode dummyHead = new ListNode(0);//给p,q 为表头数据,curr为我们最终求的链表,carry即为进位值,初始化为0ListNode 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更新进位carry = sum / 10;//为链表创建新的节点,节点初始值为sum的个位数,赋值到ListNode的结构体成员next中。//将curr.next加入链表中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;        }
}
友情链接:

wiki-linked list

leecode-解答

csdn-数据结构学习笔记之一:链表

这篇关于【2017.11.29】2.Add Two Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/824112

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.B

一个bug日志 FATAL EXCEPTION: main03-25 14:24:07.724: E/AndroidRuntime(4135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syyx.jingubang.ky/com.anguotech.android.activity.Init

ubuntu16.04 Git add 使用tab键卡死

以前使用Ubuntu14.04 进行git add 操作时使用TAB键可以很快自动补全,但自从使用16.04使用TAB半天没有反应。 一开始以为是Git版本问题,后验证与Git无关。 搜索发现与Dash有关,以下是博客原文: http://www.51testing.com/html/50/n-1245050.html 今天碰到一个问题git 后面的参数用Tab键无法补全

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

Add All -uva优先队列的应用

题目的解法属于贪心,因为cost=a1+a2,所以要保证每次的cost最小,所以说,每次将队列中最小的两个相加,得出来的数放入队列中,再取2个最小的相加,直到全部加完,所以这就涉及了一个取2个最小数的问题,我说一下我一开始的做法 #include<stdio.h>#include<iostream>#include<stdlib.h>using namespace std;#define

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

Android studio jar包多层嵌套,Add library '__local_aars__:...@jar' to classpath问题

在添加jar包,早app下的build.gradle中的 implementation files('libs/jar包的名字.jar') 修改为 api files('libs/jar包的名字.jar') implementation 单层引用,只引用当前jar包层, api 多层引用,应用当前jar包层,已经jar包引用的jar包层

unable to access android sdk add-on list解决办法

mac环境,由于不小心删掉了sdk文件夹的内容,拷贝别人的文件内容过来后,发现sdkmanager不见了。 慌乱中重装了Android Studio。 打开app后发现如下提示:unable to access android sdk add-on list 解决办法: 在 Android Studio 安装目录 bin/idea.properties 文件最后追加一句 disabl

基于Python的机器学习系列(29):前馈神经网络

在本篇文章中,我们将学习如何使用PyTorch构建和训练一个前馈神经网络。我们将以线性回归为例,逐步了解PyTorch的各个组件及其在神经网络中的应用。这些步骤包括: 指定输入和目标:我们将定义输入特征和目标变量。数据集和数据加载器:使用PyTorch的数据集和数据加载器来管理和加载数据。nn.Linear(全连接层):创建前馈神经网络中的线性层。定义损失函数:选择合适的损失函数