1. 程式人生 > >美團網2014筆試演算法題彙總

美團網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,用程式實現。

#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.一個函式access(),呼叫頻率不能超過R次/sec,用程式實現一個函式,當超過R次/sec時返回access false,不超過時返回success

#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. 一個m*n的矩陣,從左到右從上到下都是遞增的,給一個數elem,求是否在矩陣中,給出思路和代 碼.

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

4.利用兩個棧,模擬queue

#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;  
} 


5.求兩個字串的最長公共子串

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;     
    }  
}


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