vivo2020秋招提前批笔试题

vivo2020秋招提前批笔试题

(1)
种花草问题
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
题解来源:
https://leetcode-cn.com/problems/can-place-flowers/solution/zui-qing-xi-zui-jian-ji-de-cdai-ma-by-lu-guo-de-fe/

class Solution 
{
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) 
    {
        int count =  0;
        flowerbed.insert(flowerbed.begin(),0);
        flowerbed.insert(flowerbed.end(),0);
        for(int i = 1; i < flowerbed.size()-1; i++)
        {
            if(flowerbed[i] == 0 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0)
            {
                flowerbed[i] = 1;
                count++;
            }
        }
        return n <= count;
    }
};

(2)鸡蛋掉落
你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N 共有 N 层楼的建筑。
每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去。
你知道存在楼层 F ,满足 0 <= F <= N 任何从高于 F 的楼层落下的鸡蛋都会碎,从 F 楼层或比它低的楼层落下的鸡蛋都不会破。
每次移动,你可以取一个鸡蛋(如果你有完整的鸡蛋)并把它从任一楼层 X 扔下(满足 1 <= X <= N)。
你的目标是确切地知道 F 的值是多少。
无论 F 的初始值如何,你确定 F 的值的最小移动次数是多少?
题解来源:
https://leetcode-cn.com/problems/can-place-flowers/solution/zui-qing-xi-zui-jian-ji-de-cdai-ma-by-lu-guo-de-fe/

class Solution {
public:
    int calcF(int K, int T)
    {
        if (T == 1 || K == 1) return T + 1;
        return calcF(K - 1, T - 1) + calcF(K, T - 1);
    }

    int superEggDrop(int K, int N)
    {
        int T = 1;
        while (calcF(K, T) < N + 1) T++;
        return T;
    }
};

(3)合并链表
合并多个排序链表…

 ListNode* mergeKLists(vector<ListNode*>& lists) 
    {
       ListNode * head = new ListNode(0);
		int n = lists.size();
		if (n < 1)
			return NULL;
		int begin;
		for (int i = 1; i <= n; ++i)
		{
			if (lists[i-1] != NULL)
			{
				head->next = lists[i-1];
				begin = i;
				break;
			}
		}
		for (int i = begin+1; i <= n; ++i)
		{
			ListNode *k = head->next;
			ListNode *q = lists[i - 1];
			ListNode *tempHead = head;
			while (k&&q)
			{
				if (k->val <= q->val)
				{
					tempHead->next = k;
					tempHead = k;
					k = k->next;
				}
				else
				{
					tempHead->next = q;
					tempHead = q;
					q = q->next;
				}
			}
			while (k)
			{
				tempHead->next = k;
				tempHead = k;
				k = k->next;
			}
			while (q)
			{
				tempHead->next = q;
				tempHead = q;
				q = q->next;
			}
		}
		ListNode *q = head->next;
		delete head;
		return q;
    }

你可能感兴趣的:(面试总结)