zoukankan      html  css  js  c++  java
  • 【leetcode】2. 两数相加

    #define MANLEN 1000
    void recursion(struct ListNode* l1, struct ListNode* l2,int* res,int* len,int* flag,int* pst){
        if(l1 || l2){
            *(pst)=(l1)?1 :0;
            int temp1=(l1)?l1->val :0;
            int temp2=(l2)?l2->val :0;
            res[(*len)++]=( temp1 + temp2 + *flag )%10;;
            *flag = ( (temp1 + temp2 + *flag) >= 10 )?1 :0;
        }
        if(!l1 && !l2){
            if(*flag)
                res[(*len)++]=1;
            return;
        }        
        recursion((l1)?l1->next:l1,(l2)?l2->next:l2,res,len,flag,pst);
        if(*pst)
            l1->val=res[--(*len)];
        else
            l2->val=res[--(*len)];
    }
    
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
        int res[MANLEN]={0};
        int len=0, pst=0, flag=0;
        recursion(l1,l2,res,&len,&flag,&pst);
        if(len>0){
            struct ListNode* p=(struct ListNode*)calloc(sizeof(struct ListNode),1);
            p->val=res[0];
            p->next=(pst)?l1 :l2;
            return p;
        }        
        return (pst)?l1 :l2;    
    }
  • 相关阅读:
    Binary Trees
    [POJ] String Matching
    Tree
    Maxmum subsequence sum problem
    poj 2104 划分树
    poj 2486 树形dp
    poj 1848 树形dp
    hdu 4578 线段树
    hdu 4585 set应用
    hdu 2412 树形DP
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14085329.html
Copyright © 2011-2022 走看看