美团点评编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moses1213/article/details/52490599

1.输入一个数组nums,要求返回一个新的数组count,count的i位置上放置的是nums[i]右边有多少比它小的数。

例如输入: 1 2 3 4

输出:0 0 0 0 

这道题从右边倒着数比较好,美团点评的题目没有自己的测试用例,需要用户自己考虑到所有可能的情况……

#include <iostream>
#include <vector>
using namespace std;

void func(vector<int> nums, int* count)
{
	int n = nums.size();
	count[n-1] = 0;
	for(int i = n-2; i >=0; --i)
	{
		if(nums[i] > nums[i+1])
			count[i] = count[i+1] + 1;
		else
		{
			int tmp = nums[i];
			int j;
			for(int j = i+1; j < n && tmp <= nums[j]; ++j)
			count[i] = count[j];
		}
	}
}

int main() {
	// your code goes here
	vector<int> nums;
	int input;
	while(cin >> input)
		nums.push_back(input);
	int* count = new int[nums.size()];
	func(nums, count);
	for(int i = 0; i < nums.size(); ++i)
		cout << count[i] << " ";
	delete[] count;
	return 0;
}

2.给定一棵二叉查找树,输入是它的中序遍历,要求删除其中指定一个结点,输出新的二叉查找树的中序遍历。

例子:

输入:1    2    3    4    5    6    7

           1

           2

           4

输入的第一行是二叉树的中序遍历,后面每行是分别删除的结点。

输出:2    3    4    5    6    7

           1    3    4    5    6    7

           1    2    3    5    6    7

这道题说实话有点弱智,出题者难道想让我们建好二叉查找树然后删除结点维持二叉查找树吗,二叉查找树的中序遍历是递增的,删除一个结点后它的中序遍历还是递增的,所以只输出的中序遍历的话只需要简单的把原中序遍历中要删除的那个结点拿掉就可以了,代码非常简单。(也许我看出题意了?)

void func(int* array, int n, int val, int* result)
{
	int index = 0;
	for(int i = 0; i < n; ++i)
	{
		if(array[i] != val)
			result[index++] = array[i];
	}
}




猜你喜欢

转载自blog.csdn.net/moses1213/article/details/52490599