zoukankan      html  css  js  c++  java
  • 美团面试算法题

    1. Given a sorted linked list, delete all duplicates such that each element appear only once.
    
    For example,
    Given 1->1->2, return 1->2.
    Given 1->1->2->3->3, return 1->2->3.zhi
    Given 1->1->1->2->3
    ListNode* deleteElements(ListNode *head)
    {
        if(head==NULL&&head->next==NULL)
            return head;
        ListNode *p=head;
        ListNode *q=p->next;
        while(q)
        {
            if(p->val!=q->val)
            {
                listNode *del=p->next;
                while(del!=q)
                {
                    ListNode *tmp=del->next;
                    delete del;
                    del=tmp;
                }
                p->next=q;
                p=q;
                q=q->next;
            }
            else
            {
                q=q->next;
            }
        }
        p->next=NULL;
        return head;
    }
    
    2. 打印杨辉三角
         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
     
     输入:n, 打印前n行,不用考虑缩进
     
     void print(int n)
     {
         if(n<=0)
             return;
         if(n==1)
         {
             cout<<1<<endl;
             return;
         }
         if(n==2)
         {
             count<<1<<' '<<1<<endl;
             return;
         }
         vector<int> res={1,1};
         cout<<1<<endl;
         cout<<1<<' '<<1<<endl;
         for(int i=3;i<=n;++i)
         {
              vector<int> tmp;
              tmp.push_back(1);
              for(int j=0;j<res.size()-1;++j)
              {
                  tmp.push_back(res[j]+res[j+1]);
              }
              tmp.push_back(1);
              res=tmp;
              for(auto a:res)
              {
                  cout<<a<<' ';
              }
              cout<<endl;
         }
    }   
  • 相关阅读:
    继承中类的作用域
    访问控制与继承
    虚函数与抽象基类
    定义基类和派生类
    类成员指针
    固有的不可移植特性
    局部类
    union
    嵌套类
    枚举类型
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4764527.html
Copyright © 2011-2022 走看看