2. Add Two Numbers [LeetCode]

/**************************************************************************
 * 
 * 2. [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)
 * 
 * 描述:
 * 输入两个非空链表,分别表示两个非负整数,每一位数字倒序保存在链表中,返回这两个链表的和,结果同样保持倒序。
 * Example:
 * Input: l1 = [2,4,3], l2 = [5,6,4]
 * Output: [7,0,8]
 * Explanation: 342 + 465 = 807.
 * 
 * Similar Questions:66、67
 **************************************************************************/





/// 解法一 C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    if(l1 == NULL)return l2;
    if(l2 == NULL)return l1;
    struct ListNode* a = l1;
    struct ListNode* b = l2;
    struct ListNode *dum = (struct ListNode *)malloc(sizeof(struct ListNode));  
    struct ListNode *pre = dum;
    
    int inc = 0;     
    while(a || b || inc){
        int i = a ? a->val : 0;
        int j = b ? b->val : 0;
        int sum = i + j + inc;
        inc = sum / 10;
        
        struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
        pre->next = node;
        node->val = sum % 10;
        node->next = NULL;
        
        pre = node;
        a = a ? a->next : NULL;
        b = b ? b->next : NULL;
    }
    struct ListNode *res = dum->next;
    free(dum);
    dum = NULL;
    return res;
}

/**************************************************************************
 * 
 * 66. [Plus One](https://leetcode.com/problems/plus-one/)
 * 
 * Given a non-empty array of decimal digits representing a non-negative integer, 
 * increment one to the integer.
 * The digits are stored such that the most significant digit is at the head of the list, 
 * and each element in the array contains a single digit.
 * You may assume the integer does not contain any leading zero, except the number 0 itself.
 * 
 * Example 1:
 * Input: digits = [1,2,3]
 * Output: [1,2,4]
 * Explanation: The array represents the integer 123.
 * 
 * Example 2:
 * Input: digits = [9,9]
 * Output: [1,0,0]
 * Explanation: The array represents the integer 99.
 * 
 * Example 3:
 * Input: digits = [0]
 * Output: [1]
 * 
 * Similar Questions:2、67
 **************************************************************************/



/// C++
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        add(digits, 1);
        return digits;
    }
private:
    void add(vector<int>&digits, int one) {
        int carry = one;
        for (auto i = digits.rbegin(); i != digits.rend(); ++i) {
            *i += carry;
            carry = *i / 10;
            *i %= 10;
        }
        if (carry > 0)
            digits.insert(digits.begin(), 1);

    }
};





/// C
/**
 * reuse void add_to_string(char *string, char num, int index); of ./problems/43.Multiply Strings.cpp 
 */
static void plusNumAt(int* digits, int index, char num) {
    if (index < 0) return;
    int sum = digits[index] + num;
    int cur = sum % 10;
    int carry = sum / 10;
    digits[index] = cur;
    if (carry) 
        plusNumAt(digits, index - 1, carry);
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* plusOne(int* digits, int digitsSize, int* returnSize){
    int *res = (int *)malloc((digitsSize + 1) * sizeof(int));
    res[0] = 0;
    for (int i = digitsSize; i > 0; i--)
        res[i] = digits[i-1];

    plusNumAt(res, digitsSize, 1);

    if (res[0] == 0) {
        for (int i = 0; i < digitsSize; i++)
            res[i] = res[i+1];
        *returnSize = digitsSize;
        return res;
    }

    *returnSize = digitsSize + 1;
    return res;
}

 

/**************************************************************************
 * 
 * 67. [Add Binary](https://leetcode.com/problems/add-binary/)
 * 
 * Given two binary strings a and b, return their sum as a binary string.
 * 
 * Example 1:
 * Input: a = "11", b = "1"
 * Output: "100"
 * 
 * Example 2:
 * Input: a = "1010", b = "1011"
 * Output: "10101"
 * 
 * Similar Questions:2、66
 **************************************************************************/


void reverse(char *a){
    int len = strlen(a);
    if (NULL == a || len <= 1)
        return;
    
    for(int i = 0; i < len / 2; i++) {
        a[i]       = a[i] ^ a[len-1-i];
        a[len-1-i] = a[i] ^ a[len-1-i];
        a[i]       = a[i] ^ a[len-1-i];
    }
}

char * addBinary(char * a, char * b){
    int lena = strlen(a);
    int lenb = strlen(b);
    int size = lenb > lena ? lenb : lena;
    if(lena == 0 || a == NULL) return b;
    if(lenb == 0 || b == NULL) return a;
    char *result = (char *)malloc((size + 2)*sizeof(char));
    if (result == NULL) return NULL;
    memset(result, 0, (size + 2)*sizeof(char));
    reverse(a);
    reverse(b);
    int carry = 0;
    for(int i = 0; i < size; i++) {
        int ai = i < lena ? a[i] - '0' : 0;
        int bi = i < lenb ? b[i] - '0' : 0;
        int val = (ai + bi + carry) % 2;
        carry = (ai + bi + carry) / 2;
        result[i] = val + '0';
    }

    if(carry)
        result[size] = '1';

    reverse(result);
    return result;
}


class Solution {
public:
    string addBinary(string a, string b) {
        string result;
        const size_t n = a.size() > b.size() ? a.size() : b.size();
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        int carry = 0;
        
        for (int i = 0; i != n; ++i) {
            const int ai = i < a.size() ? a[i]-'0':0;
            const int bi = i < b.size() ? b[i]-'0':0;
            const int val = (ai + bi + carry) % 2;
            carry= (ai + bi + carry)/2;

            result.insert(result.begin(), val+'0');
        }
        if(carry == 1)
            result.insert(result.begin(), '1');
        return result;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值