ali2015校园招聘笔试大题

【本文链接】

 http://www.cnblogs.com/hellogiser/p/ali-2015-questions.html

1. 写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函数返回这棵二叉树中相差最大的两个节点间的差值绝对值。请注意程序效率。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/14
*/

struct BinaryTreeNode
{
    
int value;
    BinaryTreeNode *left;
    BinaryTreeNode *right;
};

void PreOrder(BinaryTreeNode *root, int &max, int &min)
{
    
if (root == NULL)
        
return;
    
if (max < root->value)
    {
        max = root->value;
    }
    
if (min > root->value)
    {
        min = root->value;
    }
    PreOrder(root->left, max, min);
    PreOrder(root->right, max, min);
}

int MaxDiff(BinaryTreeNode *root)
{
    
if (root == NULL)
        
return INT_MIN;
    
int min, max;
    min = max = root->value;
    PreOrder(root, max, min);
    
return max - min;
}

2 给定一个query和一个text,均由小写字母组成。要求在text中找出以同样的顺序连续出现在query中的最长连续字母序列的长度。例如, query为“acbac”,text为“acaccbabb”,那么text中的“cba”为最长的连续出现在query中的字母序列,因此,返回结果应该为其长度3。请注意程序效率。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/14
*/


#include "stdafx.h"
#include "iostream"
using namespace std;

void output_array(int **a, int len1, int len2)
{
    
int i, j;
    
for(i = 0; i <= len1; i++)
    {
        
for(j = 0; j <= len2; j++)
        {
            cout << a[i][j] << 
" ";
        }
        cout << endl;
    }
    cout << endl;
}

int findstring(char *query, char *text)
{
    
if(NULL == query || NULL == text)
    {
        
return -1;
    }
    
int len1 = strlen(text);
    
int len2 = strlen(query);
    
int **arrays = new int *[len1 + 1];
    
int i, j;
    
for(i = 0; i <= len1; ++i)
        arrays[i] = 
new int[len2 + 1]();
    
int max = 0;
    
for(i = 1; i <= len1; i++)
    {
        
for(j = 1; j <= len2; j++)
        {
            
if(text[i - 1] == query[j - 1])
                arrays[i][j] = arrays[i - 
1][j - 1] + 1;
            
else
                arrays[i][j] = 
0;
            
// get max of arrays
            if(max < arrays[i][j])
            {
                max = arrays[i][j];
            }
        }
    }

    output_array(arrays, len1, len2);
    
for(i = 0; i <= len1; i++)
        
delete [] arrays[i];
    
delete [] arrays;
    
return max;
}


void test_default(char *query, char *text)
{
    cout << findstring(query, text) << endl;
}

void test_case1()
{
    
char *query = "acbac";
    
char *text = "acaccbabb";
    test_default(query, text); 
// 3  cba
}

void test_main()
{
    test_case1();
}

int main()
{
    test_main();
    
return 0;
}

【参考】

http://wenku.baidu.com/link?url=m7jMJ3tAoARQg6b1zAGnr451vCGBYRVzRqC_T2Rb8WwuQl_I3uJxyhPRQXvbJ_5cef03F5indLE-qFDuFxiBCU5drNXw1EgzzQUBSSnEhiG

个人学习笔记,欢迎拍砖!---by hellogiser

Author: hellogiser
Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
原文地址:https://www.cnblogs.com/hellogiser/p/ali-2015-questions.html