美團網2014筆試演算法題匯總

來源:互聯網
上載者:User


#include <iostream>  using namespace std;    struct ListNode  {      int m_nValue;      ListNode *m_pNext;  };    ListNode* CreateList(int val)  {      ListNode *pHead = new ListNode;        pHead->m_nValue = val;      pHead->m_pNext = NULL;        return pHead;  }    void InsertNode(ListNode **pHead, int val)  {      ListNode *pNode = new ListNode;      pNode->m_nValue = val;      pNode->m_pNext = NULL;        while ((*pHead)->m_pNext != NULL)      {          (*pHead) = (*pHead)->m_pNext;      }        (*pHead)->m_pNext = pNode;      (*pHead) = pNode;  }    void PrintList(ListNode *pHead)  {      while (pHead != NULL)      {          cout<<pHead->m_nValue<<" ";          pHead = pHead->m_pNext;      }      cout<<endl;  }    ListNode* Reverse(ListNode *pHead)  {      if (pHead == NULL || pHead->m_pNext == NULL)      {          return pHead;      }        ListNode *pPre = NULL;      ListNode *pCurrent = pHead;      ListNode *pPost = pHead->m_pNext;        while (pCurrent->m_pNext != NULL)      {          pCurrent->m_pNext = pPre;          pPre = pCurrent;          pCurrent = pPost;          pPost = pPost->m_pNext;      }      pCurrent->m_pNext = pPre;        return pCurrent;  }        ListNode* ReverseList(ListNode *pHead, int k)  {      if (pHead==NULL || pHead->m_pNext==NULL)      {          return pHead;      }        ListNode *pPre = NULL;      ListNode *pCurrent = pHead;      ListNode *pPost = pHead->m_pNext;      ListNode *pStart = NULL;      ListNode *pEnd = NULL;        int n = 0;      pEnd = pCurrent;      pEnd->m_pNext = NULL;      while (pPost != NULL)      {          ++n;          if (n == (k+1))          {              pStart = pPre;              pEnd->m_pNext = ReverseList(pCurrent, k);                return pStart;          }          else          {              pCurrent->m_pNext = pPre;              pPre = pCurrent;              pCurrent = pPost;              pPost = pPost->m_pNext;          }      }        pCurrent->m_pNext = pPre;      pStart = Reverse(pCurrent);      return pStart;  }    int main()  {      ListNode *pHead = NULL;      ListNode *head = NULL;      int n;      cout<<"輸入鏈表中節點的個數 n:"<<endl;      cin>>n;      cout<<"請輸入n個整數值:"<<endl;      for (int i=0; i<n; ++i)      {          int data;          cin>>data;            if (pHead == NULL)          {              pHead = CreateList(data);              head = pHead;          }          else          {              InsertNode(&pHead, data);          }      }        int k;      cout<<"請輸入k:"<<endl;      cin>>k;      head = ReverseList(head, k);      PrintList(head);        system("pause");      return 0;  }  

2.

#define false 0  #define success 1  int getcurrentms()  {    struct timeval tv;    gettimeofday(&tv,NULL);    return tv.tv_sec*1000+tv.tv_usec/1000; //得到毫秒數  }    bool count_access()  {    static int count=0;    static int time_ms_old=0,time_ms_now;    if(count==0)    {      time_ms_old=getcurrentms();    }    count++;    access();    if(count>=R)    {      time_ms_now=getcurrentms();      if(time_ms_now-time_ms_pld>=1000)          return false;      else          return success;    }    return success;  }  
3.

解: 思路:從矩陣的右上方開始判斷即可,每次可以消除一行或一列,詳見劍指offer一書.

4.

#include <iostream>  #include <stack>  using namespace std;    template <class T>  class Queue  {  public:      Queue()      {      }      ~Queue()      {      }        void add(const T& t);      T remove();  private:      stack<T> s1;      stack<T> s2;  };    template <class T>  void Queue<T>::add(const T& t)  {      s1.push(t);  }    template <class T>  T Queue<T>::remove()  {      if (s2.size() <= 0)      {          while (s1.size() > 0)          {              T t = s1.top();              s2.push(t);              s1.pop();          }      }        if (s2.size() == 0)      {          throw new exception("empty queue");      }        T t = s2.top();      s2.pop();        return t;    }    int main()  {      Queue<char> q;        q.add('A');      q.add('B');      q.add('C');      cout<<q.remove()<<endl;      cout<<q.remove()<<endl;      cout<<q.remove()<<endl;        system("pause");      return 0;  } 


public class MaxConString {        /**      * 計算兩字串最大公用字串長度      */      public static void main(String[] args) {            char[] s1 = "jiajiangayaoyao".toCharArray();            //測試資料          char[] s2 = "jiangyaoyao".toCharArray();          int c = new MaxConString().getCount(s1, s2);          System.out.println("兩字串的共同字串長度為:"+c);                }        private int getSubCount(char[] s1,char[] s2, int i ,int j){//計算兩字串從s1的第i位置s2的第j位置的之後字串長度                                                                  //如“abc”和“ab”則返回conut為2          int count=1;          while(++i<s1.length&&++j<s2.length&&s1[i]==s2[j]){              count++;          }          return count;      }            private int getCount(char[]s1,char[]s2){                //計算兩字串的共同字串長度          int count = 0;          for(int i=0;i<s1.length;i++)              for(int j=0;j<s2.length;j++)                  if(s1[i]==s2[j]){                                            if(this.getSubCount(s1, s2, i, j)>count)                      count = this.getSubCount(s1, s2, i, j);                  }          return count;         }  }





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.