网易2016实习研发工程师编程题

网易2016实习研发工程师编程题
http://www.nowcoder.com/test/1429468/summary

题目1:小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量。这些钻石的重量各不相同。在他们们比较了一段时间后,它们看中了两颗钻石g1和g2。现在请你根据之前比较的信息判断这两颗钻石的哪颗更重。
给定两颗钻石的编号g1,g2,编号从1开始,同时给定关系数组vector,其中元素为一些二元组,第一个元素为一次比较中较重的钻石的编号,第二个元素为较轻的钻石的编号。最后给定之前的比较次数n。请返回这两颗钻石的关系,若g1更重返回1,g2更重返回-1,无法判断返回0。输入数据保证合法,不会有矛盾情况出现。
测试样例:
2,3,[[1,2],[2,4],[1,3],[4,3]],4
返回: 1

class Cmp {
public:

    int findNum(  vector< vector<int> > &mp, int value, int g, vector<int> &access )
    {
        int ret = 0;

        if( 0 == mp[value].size() )    
        {
            return ret;
        }

        int len = mp[value].size();

        for( int i = 0; i < len; i++ )
        {
            if( mp[value][i] == g )
            {
                ret = 1;
                return ret;
            }
            else
            {
                //int ret1 = 
                if( 1 == access[ mp[value][i] ] )
                {
                    continue;
                }
                else
                {
                    access[ mp[value][i] ] = 1;
                    int ret1 = findNum( mp, mp[value][i], g, access );

                    if( ret1 > 0 )
                    {
                        ret = ret1;
                        return ret;
                    }
                }
            }
        }

        return ret;
    }


    int cmp(int g1, int g2, vector<vector<int> > records, int n) {
        // write code here
        int result = 0;
        set<int> st;
        int len = records.size();

        for( int i = 0; i < len; i++ )
        {
            st.insert( records[i][0] );
            st.insert( records[i][1] );
        }

        int num = st.size();

        vector< vector<int> > mp( num + 1 );

        for( int i = 0; i < len; i++ )
        {
            mp[ records[i][0] ].push_back( records[i][1] );
        }

        if( mp[g1].size() > 0 )
        {
            int g1Len = mp[g1].size();
            vector<int> access( num + 1 );

            for( int i = 0; i < num + 1; i++ )
            {
                access[i] = 0;
            }

            for( int i = 0; i < g1Len; i++ )
            {
                if( mp[g1][i] == g2 )
                {
                    result = 1;
                    return result;
                }
                else
                {
                    access[mp[g1][i]] = 1;
                    int ret1 = findNum( mp, mp[g1][i], g2, access );

                    if( ret1 > 0 )
                    {
                        result = 1;
                        return result;
                    }
                }
            }
        }

        if( mp[g2].size() > 0 )
        {
            int g2Len = mp[g2].size();
            vector<int> access( num + 1 );


            for( int i = 0; i < num + 1; i++ )
            {
                access[i] = 0;
            }


            for( int i = 0; i < g2Len; i++ )
            {
                if( mp[g2][i] == g1 )
                {
                    result = -1;
                    return result;
                }
                else
                {
                    access[mp[g2][i]] = 1;
                    int ret1 = findNum( mp, mp[g2][i], g1, access );

                    if( ret1 > 0 )
                    {
                        result = -1;
                        return result;
                    }
                }
            }

        }

        return result;

    }
};



题目2:有一棵二叉树,树上每个点标有权值,权值各不相同,请设计一个算法算出权值最大的叶节点到权值最小的叶节点的距离。二叉树每条边的距离为1,一个节点经过多少条边到达另一个节点为这两个节点之间的距离。
给定二叉树的根节点root,请返回所求距离。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/

#include  
using namespace std;

class Tree {
public:

    // 获得从root到pTarget的路径
    int getPathFromRoot( TreeNode *root, TreeNode *pTarget, vector< TreeNode* > cur, vector< TreeNode* > &result )
    {
        if( NULL == root )
        {
            return -1;
        }

        if( root == pTarget )
        {
            cur.push_back(root);
            result.swap(cur);
            return 0;
        }

        cur.push_back(root);
        int ret = getPathFromRoot( root->left, pTarget, cur, result );

        if( 0 == ret )
        {
            return 0;
        }

        ret = getPathFromRoot( root->right, pTarget, cur, result );

        if(  0 == ret )
        {
            return 0;
        }

        cur.pop_back();

        return -1;


    }

    // 求二叉树的最大叶子节点和最小叶子节点
    void preTravel( TreeNode *root, TreeNode *&pmax, TreeNode *&pmin )
    {
        if( NULL == root )
        {
            return;
        }

        if( NULL == root->left && NULL == root->right )
        {
            if( pmax != NULL && root->val > pmax->val )
            {
                pmax = root;
            }
            else if( NULL == pmax )
            {
                pmax = root;
            } 

            if( pmin != NULL && root->val < pmin->val )
            {
                pmin = root;
            }
            else if( NULL == pmin )
            {
                pmin = root;
            } 

            return;
        }

        preTravel( root->left, pmax, pmin );
        preTravel( root->right, pmax, pmin );
        return;
    }

    // 求二叉树的最大叶子节点和最小叶子节点
    void getMaxAndMinLeaf( TreeNode *root, TreeNode * &pmax, TreeNode * &pmin )
    {
        if( NULL == root )
        {
            return;
        }

        queue< TreeNode * > myqueue;
        myqueue.push(root);

        while( !myqueue.empty() )
        {
            TreeNode *cur = myqueue.front();
            myqueue.pop();

            if( NULL == cur->left && NULL == cur->right && pmax != NULL && cur->val > pmax->val )
            {
                pmax = cur;
            }
            else if( NULL == cur->left && NULL == cur->right )
            {
                pmax = cur;
            }

            if( NULL == cur->left && NULL == cur->right && pmin != NULL && cur->val < pmin->val )
            {
                pmin = cur;
            }
            else if( NULL == cur->left && NULL == cur->right )
            {
                pmin = cur;
            }

            if( cur->left != NULL )
            {
                myqueue.push( cur->left );
            }

            if( cur->right != NULL )
            {
                myqueue.push( cur->right );
            }

        }

        return;
    }


    // 从root指向的二叉树中查找是否存在子节点p
    TreeNode *findNode( TreeNode *root, TreeNode *p )
    {
        if( NULL == root )
        {
            return NULL;
        }

        if( root == p )
        {
            return root;
        }

        TreeNode *ret = findNode( root->left, p );

        if( ret != NULL )
        {
            return ret;
        }

        ret = findNode( root->right, p );

        return ret;


    }


    //获得pmax和pmin的最低公共祖先
    TreeNode *getCommon( TreeNode *root, TreeNode *pmax, TreeNode *pmin )
    {
        if( root == NULL )
        {
            return NULL;
        }

        if( findNode( root, pmax ) != NULL && findNode( root, pmin ) != NULL )    
        {
            TreeNode *ret1 = findNode( root->left, pmax );
            TreeNode *ret2 = findNode( root->left, pmin );

            TreeNode *ret3 = findNode( root->right, pmax );
            TreeNode *ret4 = findNode( root->right, pmin );

            if( ret1 != NULL && ret2 != NULL )
            {
                return getCommon( root->left, pmax, pmin );
            }

            if( ret3 != NULL && ret4 != NULL )
            {
                return getCommon( root->right, pmax, pmin );
            }

            return root;
        }

        return NULL;
    }


    // 获得从parent到child的距离,每条边的距离为1, 若parent到child无路径,则返回-1
    int getTwoNodeDist( TreeNode *parent, TreeNode *child, int depth )
    {
        if( NULL == parent )
        {
            return -1;     
        }

        if( parent == child )
        {
            return depth;
        }

        depth++;

        int ret = getTwoNodeDist( parent->left, child, depth );

        if( ret != -1 )
        {
            return ret;
        }

        ret = getTwoNodeDist( parent->right, child, depth );

        if( ret != -1 )
        {
            return ret;
        }

        return -1;

    }


    int getDis(TreeNode* root) {
        // write code here
        int dist = 0;

        if( NULL == root )
        {
            return dist;
        }

        TreeNode *pmax = NULL;
        TreeNode *pmin = NULL;

        preTravel( root, pmax, pmin );

        TreeNode *lastCommonNode = getCommon( root, pmax, pmin );

        if( NULL == lastCommonNode )
        {
            return 4;
        }

        int dist1 = getTwoNodeDist( lastCommonNode, pmax, 0 );
        int dist2 = getTwoNodeDist( lastCommonNode, pmin, 0 );


        return dist1 + dist2;


    }
};





题目3:有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数。
给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在。
测试样例:
[1,3,5,2,2],5,3
返回:2


class Finder {
public:

    int partition( vector<int> &a, int low, int high )
    {
        int save = a[low];

        while( low < high )
        {
            while( low < high && a[high] <= save )
            {
                high--;
            }

            a[low] = a[high];

            while( low < high && a[low] >= save )
            {
                low++;
            }

            a[high] = a[low];
        }

        a[low] = save;
        return low;
    }


    int findKth(vector<int> a, int n, int K) {
        // write code here

        if( K < 1 || K > n )
        {
            return -1;
        }


        int pos = partition( a, 0, n - 1 );

        while( pos != K - 1 )
        {
            if( pos > K - 1 )
            {
                pos = partition( a, 0, pos - 1 );
            }
            else
            {
                pos = partition( a, pos + 1, n - 1 );
            }
        }

        return a[pos];
    }
};

你可能感兴趣的:(笔试/面试)