9.25 蔚来笔试AK代码

1. 两个for暴力

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个整数数组arr和一个数K,返回数组中小于K的最大的两个数之和。如果不存在小于K的两数之和,返回-1。
     * @param arr int整型vector 数组
     * @param k int整型 相加的上限
     * @return int整型
     */
    int getMax(vector<int>& arr, int k) {
        // write code here
        int n = arr.size();
        int ans = -1;
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                if(arr[i] + arr[j] < k ) {
                    ans = max(ans, arr[i] + arr[j]);
                }
            }
        }
        return ans;
    }
};

2. 模拟

注意集合中可能是字符串而不是字符,比如"{abc,def}"

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return string字符串vector
     */
    vector<string> ans;
    vector<vector<string>> a;
    string k = "";
    void dfs(int idx, string ss) {
        if(idx == a.size()) {
            ans.push_back(ss);
            return ;
        }
        for(auto &x: a[idx]) {
            dfs(idx + 1, ss + x);
        }
    }
    vector<string> split(string str) {
        // write code here
        vector<string> now;
        int n = str.size();
        for(int i = 0; i < n; i++) {
            if(str[i] == '{') {
                string ss = "";
                while(i + 1 < n && str[i+1]!='}') {
                    if(str[i+1]!=',') {
                         ss += str[i+1]; 
                    } else {
                        now.push_back(ss);
                        ss = "";
                    }
                    ++i;
                }
                now.push_back(ss);
                a.push_back(now);
                now.clear();
                ++i;
            } else {
                string ss = "";
                while(i < n && str[i] != '{') {
                   ss += str[i++];
                }
                now.push_back(ss);
                a.push_back(now);
                now.clear();
                --i;
            }
        }
        dfs(0,"");
        return ans;
    }
};

3. 拓扑排序

const int N = 1e5;
int d[N],dp[N];
vector<int> e[N];
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param N int整型 任务个数
     * @param relations int整型vector<vector<>> 依赖关系
     * @return int整型
     */
    int calcEffort(int n, vector<vector<int> >& a) {
        int m = a.size();
        for(auto &x: a) {
            e[x[0]].push_back(x[1]);
            d[x[1]]++;
        }
        queue<int> que;
        for(int i = 1; i <= n; i++) {
            if(!d[i]) {
                que.push(i);
                dp[i] = 1;
            }
        }
        int cnt = 0;
        int ans = 0;
        while(!que.empty()) {
            ++cnt;
            int x = que.front();
            que.pop();
            for(auto &y: e[x]) {
                if(--d[y] == 0) {
                    dp[y] = max(dp[y], dp[x] + 1);
                    ans = max(ans, dp[y]);
                    que.push(y);
                }
            }
        }
        if(cnt != n) {
            ans = -1;
        }
        return ans;
    }
};
#蔚来笔试##蔚来汽车#
全部评论
确实第二题比较坑,刚开始按字符做的过了71%,改成字符串就AC了
1 回复
分享
发布于 2021-09-25 21:18
emmm,感谢分享,不过好像我们考的不是一套题😅
1 回复
分享
发布于 2021-09-25 21:44
秋招专场
校招火热招聘中
官网直投
第一题用双指针好点
1 回复
分享
发布于 2021-09-25 21:48
点赞 回复
分享
发布于 2021-09-25 22:15
艹,没考虑是字符串
点赞 回复
分享
发布于 2021-09-25 22:28
大佬,大佬
点赞 回复
分享
发布于 2021-09-25 22:53

相关推荐

点赞 评论 收藏
转发
4 7 评论
分享
牛客网
牛客企业服务