[Title] algorithm algorithm topic and ideas Leetcode: two numbers together

LeetCode brush on a topic title algorithm based on the comparison, a start can be solved the problem, but in the problem solving process by determining if a relatively large number, the code looks relatively poor, after reflection and improving the original algorithm optimizes .

 

topic:

We are given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit.
 
If we add up these two numbers, it will return a new list and to represent them.
 
You can assume that in addition to the numbers 0, these two numbers will not begin with 0.
 
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Cause: 342 + 465 = 807
 

Ideas:

Because the two are not necessarily the same as the size of the list, so this is not conducive to the cycle time to calculate the number of the same position, so take steps to make 0:
 
A list of the original: 1 -> 5 -> 3 -> 2 = 2351
Two original lists: 5 -> 6 -> 9 = 965
 
After fill 0:
 
A list of the original: 1 -> 5 -> 3 -> 2 -> null = 2351
Two original lists: 5 -> 6 -> 9 -> 0 -> null = 0965
 
Such fill size 0 has not changed after a few, but better processing cycle time, not to judge the same node on the two lists is empty one.
 
And then a loop of linked list traversal, the same two digital computing nodes are added, the sum of the number assigned to the same node of the new list. If there is a carry, the value added reserved bits, the next time through, when two nodes must add an addition.
 
After the list of zero with the above, we can see the end of the last round, when the next list and a list for the next two null, in turn, said that when the list does not mean that one next two next to the list has been circulating.
 
Paste the code below:
 
/ ** 
 . * Definition List for Singly-linked 
 * public class ListNode { 
 * int Val; 
 * ListNode Next; 
 * ListNode (int X) {X = Val;} 
 *} 
 * / 
class Solution {
     public ListNode addTwoNumbers (L1 ListNode, L2 ListNode) { 
        
        // node is the first node, node.next real start node 
        ListNode node = new new ListNode (0 );
         // 0 nodes, wherein a node of any length node inconsistent with 0 added 
        ListNode = ZERO new new ListNode (0 );
         // pointer 
        ListNode Point = Node; 
        
        // + node1 + node2 level value by adding the sum 
        int sum = 0;
        // 进位标志
        int level = 0;
        
        do{           
            point.next = new ListNode(0);
            point = point.next;
            
            sum = l1.val + l2.val + level;
            level = sum / 10;
            point.val = sum % 10;
            
            l1 = l1.next == null ? zero : l1.next;
            l2 = l2.next == null ? zero : l2.next;
            
        } while(l1 != l2);
        
        if(level == 1) {
            point.next = new ListNode(1);
        }
        
        return node.next;
    }
}

 

Let us focus here

(1) level = sum / 10: 10 of the divisible, then get into the digits, if the sum is less than 10, we get 0.

(2) point.val = sum% 10: 10 to I to obtain the value of a digit.

 

 
 
 

Guess you like

Origin www.cnblogs.com/nicojerry/p/10941927.html