LeetCode 2 Add Two Numbers-- by analog adder chain

Today's talk is a classical algorithm problem, although not difficult, but very interesting, we together look at the topic:
 
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.
 
translation
 
Given two non-empty list, representatives of two non-negative integers. Both integers are stored in flashback, he asked to return a list showing these two integers.
 
Sample
 
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
 
answer
 
Topic difficult to understand, the idea is also very easy to want to understand, in fact, students play simulated vertical addition and subtraction calculation method to calculate the sum of two integers. However, since it is an adder, the carry may involve, for example two digit and may be a three-digit number. Another difficulty is the need to use the list to achieve, involving a linked list, coding complexity will be even greater.
 
Before you start coding, let's take a closer look examples:
If you are familiar with the use of C ++ STL or any other language tool libraries, presumably it should be easy to think you can use the map. map is a very common container used to store data format of key-value pairs.
 
We found that, due to the reverse order of the list stored in digital and conventional order, so if there is a carry case, we only need the end of the list to add a node. Typically calculated from right to left, in which the list can be understood from left to right, in fact, is calculated in accordance with the order of the traversal list. I want to understand this point, and there is no problem.
 
We do a summary, there are about three difficulties:
 
  • Because it is not an array, so we can not get the length of the list. There will be two lists of length is inconsistent
  • Return result is a linked list, we need to manually create
  • Carry calculation process generated
 
We analyze one by one, the length of the list is unknown handled well, we need only determine whether the next node of the current node is empty, you can know whether the list and there are a successor. If not, then it means that the list has been traversed coming to an end.
 
Since there are two lists among this question, we need to determine whether they end:
 
 
Manually create the list is not complicated, we first create a list of nodes, followed by insertion into the rear of the node to node. Insert list is also very simple way, assuming that the current node is a cur, a node is a node to be inserted, then we only need cur.next point node, and then assigned to a node can cur as:
 

 

 

Carry calculating the resulting problem is even simpler, if the carry bit occurs before we are due to calculate bit adder, so we can be labeled with a variable. If there is, then the current calculations plus one. Because the addition calculation, the results can only reach a maximum of 19 (plus two forward position on the 9), so carry only add up to one. Finally, determine what the current results of a carry-bit computing can be.
 
 
Finally, we just need the three points above comes together, you can write code. If the coding or have difficulty in public No. TechFlow reply LeetCode2, get the code.
It is not over, which in most languages, int there is scope. Typically 32 bits, if int64 is 64 bits. This is the computer's CPU and bandwidth are related, that have not thought about, over 64 binary integer that can be represented how should I do it?
其实就是用类似本题当中的方法,通过链表将每一位串联起来,在计算加减乘除的时候,则是像人工打竖式那样去计算。这种算法称为高精度。感兴趣的同学,可以自行搜索,以后有机会,会在之后的文章里更新。
 
如果喜欢本文,请顺手扫码给个关注吧:
 

Guess you like

Origin www.cnblogs.com/techflow/p/12119088.html