当前位置: 首页 > news >正文

美团网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;     
    }  
}

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




相关文章:

  • 2014百度校招笔试题
  • 从程序员到项目经理:认识项目经理
  • Struts2SpringHibernate整合示例,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)...
  • 每天一道算法_2_求高精度幂
  • 【总结】android程序不显示图标,开机自动启动?我来告诉你
  • 完美洗牌算法学习
  • 关于完美洗牌算法中圈和圈起点的一个证明
  • 关于完美洗牌问题的若干思考
  • 木块砌墙
  • Google的一个面试题——数组原地置换
  • linux hostname命令学习
  • 程序锁的核心基本原理
  • 每天一道算法_3_487-3279_对电话号码格式化统计批处理
  • linux编译和运行一个可执行文件初学篇
  • 关于转义字符的学习
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Android Volley源码解析
  • C++11: atomic 头文件
  • Debian下无root权限使用Python访问Oracle
  • eclipse(luna)创建web工程
  • EOS是什么
  • happypack两次报错的问题
  • Mybatis初体验
  • node和express搭建代理服务器(源码)
  • select2 取值 遍历 设置默认值
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • 从零搭建Koa2 Server
  • 构建工具 - 收藏集 - 掘金
  • 力扣(LeetCode)22
  • 线性表及其算法(java实现)
  • hi-nginx-1.3.4编译安装
  • kubernetes资源对象--ingress
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • #pragma data_seg 共享数据区(转)
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • ()、[]、{}、(())、[[]]命令替换
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (SpringBoot)第二章:Spring创建和使用
  • (备忘)Java Map 遍历
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (转) Face-Resources
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转载)Linux网络编程入门
  • ..回顾17,展望18
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .NET Reactor简单使用教程
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .NET导入Excel数据
  • @RequestBody与@ResponseBody的使用
  • [ 蓝桥杯Web真题 ]-Markdown 文档解析
  • [.NET 即时通信SignalR] 认识SignalR (一)
  • [2016.7 day.5] T2