C++ 3ms AC ! anyone else?


  • 0

    anyone else? using myself_list

    class Solution {
        public:
          struct node
          {
           char val;
           node *next;
           node *pre;
           int isnum;
        node ()
         {
            next=NULL;
            pre=NULL;
             isnum=0;
          }
        };
    void insertPre(node * cur,char c,int isnum )   
    {
        node * curpre =new node();  //new node
        curpre->val=c;
        curpre->isnum=isnum;
        cur->pre->next=curpre;    //insert
        curpre->pre=cur->pre;
        curpre->next=cur;
        cur->pre=curpre;
    }
    string int2str(int a)
    {
        string ans;
        while(a!=0)
            {
                ans=char('0'+a%10)+ans;
                a/=10;
            }
        return ans;
    }
    string findContestMatch(int n) 
    {
         node *cur=new node();
         node *end=new node();
         node *head=new node();
         cur->val='x';
         cur->isnum=1;
         cur->next=end;
         cur->pre=head;
         end->pre=cur;
         head->next=cur;
         for(int i=2;i<=n;i=i*2)
         {
             cur=head->next;
             while(cur!=NULL)
             {
                 if(cur->isnum)
                 {
                     int num=cur->isnum;
                     insertPre(cur,'(',0);
                     cur=cur->next;
                     insertPre(cur,',',0);
                     insertPre(cur,'x',i+1-num);
                     insertPre(cur,')',0);
                 }
                 cur=cur->next;
             }
         }
         string ans;
         cur=head->next;
         while(cur!=end)
         {
             if(cur->isnum)
                 ans+=int2str(cur->isnum);
             else 
                 ans+=cur->val;
             cur=cur->next;
         }
         return ans;
     }
     };

  • 0

    code may be too long...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.