美团点评编程题

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值