Linked list or string simulation addition, plus one and multiplication

Source: Internet
Author: User

Linked list analog addition/string simulation binary addition/array simulation plus an operation/print 1 to the largest n-digit/string analog multiplication

============================================

ADD two Numbers

Two lists represent two digits, each node's value is a number, and the single linked list holds the two numbers in reverse order,

Construct a new linked list that represents the and of the two linked lists.

The tail interpolation method of the linked list, the use of the dummy node of the head node, the operation of the Prev pointer is unified,

/** * Definition for singly-linked list.
 * struct ListNode {* int val;
 * ListNode *next;
 * ListNode (int x): Val (x), Next (NULL) {} *}; * * Class Solution {public:listnode *addtwonumbers (ListNode *l1, ListNode *l2) {/* header node, dummy mainly to maintain the prev of the unified
        One operation * * ListNode dummy (-1);
        int carry = 0;
        ListNode *prev = &dummy;
        ListNode *p1 = L1, *p2 = L2;
            while (p1!= null | | | P2!= NULL) {const int acur = (P1 = null)? 0:p1->val; const int bcur = (P2 = NULL)?
            0:p2->val;
            const int value = (Acur + bcur + carry)% 10;
            Carry = (Acur + bcur + carry)/10;
    
            * * Prev->next = new ListNode (value); P1 = (P1 = NULL)?
            null:p1->next; P2 = (P2 = NULL)?
            null:p2->next;
        Prev = prev->next;
        } if (Carry > 0) {prev->next = new ListNode (carry);return dummy.next; }
};

ADD Binary

Two strings to simulate binary addition, binary strings are stored in the positive sequence, different from the list of analog additive problem,

When inserting, there is a head interpolation method and a trailing interpolation method. This is obviously going to be a head plug.

Class Solution
{public
:
    string Addbinary (String A, string b)
    {
        string result;
        const int n = a.size () > B.size ()? A.size (): B.size ();
        /* Reverse over, then compute from left to right
        /reverse (A.begin (), A.end ());
        Reverse (B.begin (), B.end ());
    
        int carry = 0;
        for (int i = 0; i < n; ++i)
        {/
            * has one side, exceeding the length to set this party to 0*/
            const int acur = i < A.size ()? A[i]-' 0 ': 0;< c15/>const int bcur = i < B.size ()? B[i]-' 0 ': 0;
            /* Remainder *
            /const int val = (acur + bcur + carry)% 2;
            /* Carry * *
            carry = (Acur + bcur + carry)/2;
    
            /* head interpolation * *
            Result.insert (Result.begin (), Val + ' 0 ');
        }
        if (carry = = 1)
        {
            Result.insert (Result.begin (), ' 1 ');
        }
        return result;
    }
;

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.