高精度【模拟大数的运算题目汇总】

Add Two Numbers

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
思路:
考虑

  • {NULL, NULL}
  • {NULL, [1,2]}
  • {[1,2], NULL}
  • {[3,6, 9,9,9,9], [1,4]}
  • {[1,4], [3,6, 9,9,9,9]}
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int carry = 0, sum;
        ListNode* res = l1, *endOfRes = l1; // l1 = l1+l2
        while(l1 && l2){
            sum = l1->val+l2->val+carry;
            carry = sum/10;
            l1->val = sum%10;
            endOfRes = l1;
            l1 = l1->next;
            l2 = l2->next;
        } if(l2){
            if(endOfRes)
                endOfRes->next = l2;
            else   // l1 is NULL
                res = l2;
        } while(carry){
            if(endOfRes->next){
                endOfRes = endOfRes->next;
                sum = endOfRes->val+carry;
                carry = sum/10;
                endOfRes->val = sum%10;
            } else{
                ListNode* node = new ListNode(carry);
                carry = 0; // !!! don't forget
                endOfRes->next = node;
            }
        } return res;
    }
};

Add Binary

Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = “11”
b = “1”
Return “100”.

class Solution {
public:
    string addBinary(string a, string b) {
        int size = max(a.length(), b.length())+1; // at most one more bit after add
        int i, lenA = a.size()-1, lenB = b.size()-1;
        string res(size, '0');
        bool carry = 0;
        for(i = size-1; lenA >= 0 && lenB >= 0; i--, lenA--, lenB--){
            if(a[lenA] == b[lenB]){ // 0+0 or 1+1
                res[i] = '0'+ carry;
                carry = a[lenA]-'0';
            } else{  // 0 + 1 + carry
                if(!carry){
                   res[i] = '1'; // carry = 0;
                } //else { carry = 1; res[i] = '0';}
            }
        }
        while(lenA >= 0){
            if(a[lenA]-'0' == carry){
                i--; // res[i--] = '0', carry = carry
            } else{
                res[i--] = '1';
                carry = 0;
            } lenA--;
        }while(lenB >= 0){
            if(b[lenB]-'0' == carry){
                i--; // res[i--] = '0', carry = carry
            } else{
                res[i--] = '1';
                carry = 0;
            }lenB--;
        } if(carry){
            res[0] = '1';
        } else{
            res = res.substr(1); // start from index 1 to end
        } return res;
    }
};

代码写太长了我后续要改进

Multiply Strings

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) {
        int len1 = num1.length(), len2 = num2.length();
        if(len1 == 0 || len2 == 0) return ""; // invalid
        string res(len1*len2, 0);
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());
        for(int i = 0; i < len1; i++) num1[i] -= '0';
        for(int i = 0; i < len2; i++) num2[i] -= '0';
        int carry;
        for(int i = 0; i < len1; i++){
        	carry = 0;
            for(int j = 0; j < len2; j++){
                int ans = num1[i]*num2[j]+carry+res[i+j];
                res[i+j] = ans%10;
                carry = ans/10;
            } if(carry) res[i+len2] += carry; // 不会连续进位么?
        }
        int index;
        for(index = len1+len2-1; index >= 0 && res[index] == 0; index--);
        if(index >= 0){
            string str(index+1, '0');
            for(int i = 0; i <= index; i++) str[i] += res[index-i];
            return str;
        } return "0";
    }
    
    
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值