美团网2014笔试算法题汇总

1.链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现。

[cpp]  view plain copy
  1. #include <iostream>    
  2. using namespace std;    
  3.     
  4. struct ListNode    
  5. {    
  6.     int m_nValue;    
  7.     ListNode *m_pNext;    
  8. };    
  9.     
  10. ListNode* CreateList(int val)    
  11. {    
  12.     ListNode *pHead = new ListNode;    
  13.     
  14.     pHead->m_nValue = val;    
  15.     pHead->m_pNext = NULL;    
  16.     
  17.     return pHead;    
  18. }    
  19.     
  20. void InsertNode(ListNode **pHead, int val)    
  21. {    
  22.     ListNode *pNode = new ListNode;    
  23.     pNode->m_nValue = val;    
  24.     pNode->m_pNext = NULL;    
  25.     
  26.     while ((*pHead)->m_pNext != NULL)    
  27.     {    
  28.         (*pHead) = (*pHead)->m_pNext;    
  29.     }    
  30.     
  31.     (*pHead)->m_pNext = pNode;    
  32.     (*pHead) = pNode;    
  33. }    
  34.     
  35. void PrintList(ListNode *pHead)    
  36. {    
  37.     while (pHead != NULL)    
  38.     {    
  39.         cout<<pHead->m_nValue<<" ";    
  40.         pHead = pHead->m_pNext;    
  41.     }    
  42.     cout<<endl;    
  43. }    
  44.     
  45. ListNode* Reverse(ListNode *pHead)    
  46. {    
  47.     if (pHead == NULL || pHead->m_pNext == NULL)    
  48.     {    
  49.         return pHead;    
  50.     }    
  51.     
  52.     ListNode *pPre = NULL;    
  53.     ListNode *pCurrent = pHead;    
  54.     ListNode *pPost = pHead->m_pNext;    
  55.     
  56.     while (pCurrent->m_pNext != NULL)    
  57.     {    
  58.         pCurrent->m_pNext = pPre;    
  59.         pPre = pCurrent;    
  60.         pCurrent = pPost;    
  61.         pPost = pPost->m_pNext;    
  62.     }    
  63.     pCurrent->m_pNext = pPre;    
  64.     
  65.     return pCurrent;    
  66. }    
  67.     
  68.     
  69.     
  70. ListNode* ReverseList(ListNode *pHead, int k)    
  71. {    
  72.     if (pHead==NULL || pHead->m_pNext==NULL)    
  73.     {    
  74.         return pHead;    
  75.     }    
  76.     
  77.     ListNode *pPre = NULL;    
  78.     ListNode *pCurrent = pHead;    
  79.     ListNode *pPost = pHead->m_pNext;    
  80.     ListNode *pStart = NULL;    
  81.     ListNode *pEnd = NULL;    
  82.     
  83.     int n = 0;    
  84.     pEnd = pCurrent;    
  85.     pEnd->m_pNext = NULL;    
  86.     while (pPost != NULL)    
  87.     {    
  88.         ++n;    
  89.         if (n == (k+1))    
  90.         {    
  91.             pStart = pPre;    
  92.             pEnd->m_pNext = ReverseList(pCurrent, k);    
  93.     
  94.             return pStart;    
  95.         }    
  96.         else    
  97.         {    
  98.             pCurrent->m_pNext = pPre;    
  99.             pPre = pCurrent;    
  100.             pCurrent = pPost;    
  101.             pPost = pPost->m_pNext;    
  102.         }    
  103.     }    
  104.     
  105.     pCurrent->m_pNext = pPre;    
  106.     pStart = Reverse(pCurrent);    
  107.     return pStart;    
  108. }    
  109.     
  110. int main()    
  111. {    
  112.     ListNode *pHead = NULL;    
  113.     ListNode *head = NULL;    
  114.     int n;    
  115.     cout<<"输入链表中节点的个数 n:"<<endl;    
  116.     cin>>n;    
  117.     cout<<"请输入n个整数值:"<<endl;    
  118.     for (int i=0; i<n; ++i)    
  119.     {    
  120.         int data;    
  121.         cin>>data;    
  122.     
  123.         if (pHead == NULL)    
  124.         {    
  125.             pHead = CreateList(data);    
  126.             head = pHead;    
  127.         }    
  128.         else    
  129.         {    
  130.             InsertNode(&pHead, data);    
  131.         }    
  132.     }    
  133.     
  134.     int k;    
  135.     cout<<"请输入k:"<<endl;    
  136.     cin>>k;    
  137.     head = ReverseList(head, k);    
  138.     PrintList(head);    
  139.     
  140.     system("pause");    
  141.     return 0;    
  142. }    

2.一个函数access(),调用频率不能超过R次/sec,用程序实现一个函数,当超过R次/sec时返回access false,不超过时返回success

[cpp]  view plain copy
  1. #define false 0    
  2. #define success 1    
  3. int getcurrentms()    
  4. {    
  5.   struct timeval tv;    
  6.   gettimeofday(&tv,NULL);    
  7.   return tv.tv_sec*1000+tv.tv_usec/1000; //得到毫秒数    
  8. }    
  9.     
  10. bool count_access()    
  11. {    
  12.   static int count=0;    
  13.   static int time_ms_old=0,time_ms_now;    
  14.   if(count==0)    
  15.   {    
  16.     time_ms_old=getcurrentms();    
  17.   }    
  18.   count++;    
  19.   access();    
  20.   if(count>=R)    
  21.   {    
  22.     time_ms_now=getcurrentms();    
  23.     if(time_ms_now-time_ms_pld>=1000)    
  24.         return false;    
  25.     else    
  26.         return success;    
  27.   }    
  28.   return success;    
  29. }    
3. 一个m*n的矩阵,从左到右从上到下都是递增的,给一个数elem,求是否在矩阵中,给出思路和代 码.

解: 思路:从矩阵的右上角开始判断即可,每次可以消除一行或一列,详见剑指offer一书.

4.利用两个栈,模拟queue

[cpp]  view plain copy
  1. #include <iostream>    
  2. #include <stack>    
  3. using namespace std;    
  4.     
  5. template <class T>    
  6. class Queue    
  7. {    
  8. public:    
  9.     Queue()    
  10.     {    
  11.     }    
  12.     ~Queue()    
  13.     {    
  14.     }    
  15.     
  16.     void add(const T& t);    
  17.     T remove();    
  18. private:    
  19.     stack<T> s1;    
  20.     stack<T> s2;    
  21. };    
  22.     
  23. template <class T>    
  24. void Queue<T>::add(const T& t)    
  25. {    
  26.     s1.push(t);    
  27. }    
  28.     
  29. template <class T>    
  30. T Queue<T>::remove()    
  31. {    
  32.     if (s2.size() <= 0)    
  33.     {    
  34.         while (s1.size() > 0)    
  35.         {    
  36.             T t = s1.top();    
  37.             s2.push(t);    
  38.             s1.pop();    
  39.         }    
  40.     }    
  41.     
  42.     if (s2.size() == 0)    
  43.     {    
  44.         throw new exception("empty queue");    
  45.     }    
  46.     
  47.     T t = s2.top();    
  48.     s2.pop();    
  49.     
  50.     return t;    
  51.     
  52. }    
  53.     
  54. int main()    
  55. {    
  56.     Queue<char> q;    
  57.     
  58.     q.add('A');    
  59.     q.add('B');    
  60.     q.add('C');    
  61.     cout<<q.remove()<<endl;    
  62.     cout<<q.remove()<<endl;    
  63.     cout<<q.remove()<<endl;    
  64.     
  65.     system("pause");    
  66.     return 0;    
  67. }   


5.求两个字符串的最长公共子串

[java]  view plain copy
  1. public class MaxConString {    
  2.     
  3.     /**  
  4.      * 计算两字符串最大公共字符串长度  
  5.      */    
  6.     public static void main(String[] args) {    
  7.     
  8.         char[] s1 = "jiajiangayaoyao".toCharArray();            //测试数据    
  9.         char[] s2 = "jiangyaoyao".toCharArray();    
  10.         int c = new MaxConString().getCount(s1, s2);    
  11.         System.out.println("两字符串的共同字符串长度为:"+c);    
  12.             
  13.     }    
  14.     
  15.     private int getSubCount(char[] s1,char[] s2, int i ,int j){//计算两字符串从s1的第i位置s2的第j位置的之后字符串长度    
  16.                                                                 //如“abc”和“ab”则返回conut为2    
  17.         int count=1;    
  18.         while(++i<s1.length&&++j<s2.length&&s1[i]==s2[j]){    
  19.             count++;    
  20.         }    
  21.         return count;    
  22.     }    
  23.         
  24.     private int getCount(char[]s1,char[]s2){                //计算两字符串的共同字符串长度    
  25.         int count = 0;    
  26.         for(int i=0;i<s1.length;i++)    
  27.             for(int j=0;j<s2.length;j++)    
  28.                 if(s1[i]==s2[j]){    
  29.                         
  30.                     if(this.getSubCount(s1, s2, i, j)>count)    
  31.                     count = this.getSubCount(s1, s2, i, j);    
  32.                 }    
  33.         return count;       
  34.     }    
  35. }  


6.1.将1-7个数字的全排列按照从小到大的顺序放在一个数组,例如第0个元素是1234567,第1个是1234576,第5039个是7654321.请问第1646个元素是多少?

答案:3265417.


7.6位数字且第一位不为0的美团券密码,在易个液晶数字设备上显示,倒过来看与原,ima一样的概率是多少(如129621)(液晶显示的数字1倒过来也算一样哦)。


8.求单链表的倒数第K个元素。

struct node
{
int key;
struct node* next;
};


typedef node* List;
实现该函数
int findLastKthElement(List list, int k)。


9.现有实数数组A和B,希望将A和B归并为一个有序数组C,且C中无重复的数,请写出算法并给出算法复杂度。


10.如果两个正整数a和b,a的所有真因子之和等于b,b的所有真因子之和等于a,则称a,b是amicable pair(说明:真因子包括1但不包括本身,比如14的真因子为1、2、7)。例如220和284就是amicable pair。
请写一段代码,打印出所有不超过1000万的amicable pair。





转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/12209101

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值