• 美团面试算法题


    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;
         }
    }   
  • 相关阅读:
    谈谈焦虑
    Bridge
    Abstract Factory
    开篇辞
    缓存穿透和缓存雪崩
    缓存与数据库的一致性思考
    GDB
    代码格式化
    CSMA/CA协议
    18年秋招(19届)复习指导+面经总结,手把手教你进大厂——阿里、网易、京东、多益等
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4764527.html
Copyright © 2020-2023  润新知