leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

https://leetcode.com/problems/multiply-strings/

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note: The numbers can be arbitrarily large and are non-negative.

 

class Solution {
public:
    string multiply(string num1, string num2) {
        vector<int> a, b;
        
        for(int i=num1.length()-1;i>=0;--i) a.push_back(num1[i] - '0');
        for(int i=num2.length()-1;i>=0;--i) b.push_back(num2[i] - '0');
        
        vector<int> res(a.size()+b.size());
        for(int i=0;i<res.size();++i) res[i] = 0;
        
        for(int i=0;i<a.size();++i) {
            for(int j=0;j<b.size();++j) {
                res[i+j] += a[i] * b[j];
            }
        }
        
        for(int i=0;i<res.size();++i) {
            if(res[i]>=10) {
                res[i+1] += res[i]/10;
                res[i] = res[i]%10;
            }
        }
        //for(int i=0;i<res.size();++i) cout<<res[i]<<" ";
        //cout<<endl;
        while(res[res.size()-1] == 0) {
            if(res.size() == 1) break;
            res.pop_back();
        }
        
        string sres = "";
        for(int i=res.size()-1;i>=0;--i) sres += res[i] + '0';
        return sres;
    }
};
View Code

 

https://leetcode.com/problems/add-two-numbers/

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

 

/**
 * 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) {
        vector<int> na; na.clear();
        vector<int> nb; nb.clear();

        while(l1) {
            na.push_back(l1->val);
            l1 = l1->next;
        }
        
        while(l2) {
            nb.push_back(l2->val);
            l2 = l2->next;
        }
    
        vector<int> sum(max(na.size(), nb.size()) + 1);
        for(int i=0;i<sum.size();++i) sum[i] = 0;
        
        int i=0;
        for(;i<min(na.size(), nb.size());++i) sum[i] = na[i] + nb[i];
        for(;i<max(na.size(), nb.size());++i) sum[i] = max(na.size(), nb.size())==na.size() ? na[i]: nb[i];
        
        for(int i=0;i<sum.size()-1;++i) {
            if(sum[i] >= 10) {
                sum[i+1] += sum[i] / 10;
                sum[i] = sum[i] % 10;
            }
        }
        
        
        if(sum[sum.size()-1] == 0) sum.pop_back();
        
        ListNode *head = new ListNode(-1);
        ListNode *r = head, *s;
        for(int i=0;i<sum.size();++i) {
            s = new ListNode(-1);
            s->val = sum[i];
            r->next = s;
            r = s;
        }r->next = NULL;
        
        head = head->next;
        
        return head;
    }
};
View Code

 

posted @ 2015-11-17 14:39  流白  阅读(238)  评论(0编辑  收藏  举报