华为2012实验室算法工程师面试

懒死了,简单写了。

一面

图1
图2
图3

bool cmp(const vector<int>& a, const vector<int>& b) {
    if (a[1] == b[1]) {
        return a[0] > b[0];
    }
    return a[1] > b[1];
}

class Solution {
public:
    vector<int> filterRestaurants(vector<vector<int>>& restaurants, int veganFriendly, int maxPrice, int maxDistance) {
        vector<int> ans;
        vector<vector<int>> goodRestaurants;

        for(auto& res: restaurants) {
            if (veganFriendly && res[2] == false) {
                continue;
            }
            if (res[3] > maxPrice) {
                continue;
            }
            if (res[4] > maxDistance) {
                continue;
            }

            goodRestaurants.push_back(res);
        }

        sort(goodRestaurants.begin(), goodRestaurants.end(), cmp);

        for(auto& res: goodRestaurants) {
            ans.push_back(res[0]);
        }
        return ans;
    }
};

二面

一个三维坐标系中有n个点,问是否有三点共线。

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

const int maxn = 1000005;
struct node {
    int x, y, z;
} a[maxn];
struct xl {
    int y, z;
    double yz;
    bool xz_zero;
};

int main(int argc, const char * argv[])
{
    int n;
    bool ans = false;
    for (int i = 0; i < n; i++) {
        map<xl, bool> mp;
        for (int j = i + 1; j < n; j++) {
            int x = a[j].x - a[i].x;
            int y = a[j].y - a[i].y;
            int z = a[j].z - a[i].z;
            xl b;
            b.y = 0; b.z = 0; b.yz = 0; b.xz_zero = 0;
            if (x == 0) {
                if (z == 0) {
                    b.xz_zero = true;
                } else {
                    b.yz = y / z;
                }
            } else {
                b.y = y / x;
                b.z = z / x;
            }
            
            if (mp[b]) {
                ans = true;
                break;
            } else {
                mp[b] = true;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

主管面

聊了一下团队在干嘛
hr说让我重新做下综合/性格测试

赞赏