8.8 美团笔试AK代码


题目较为简单,但是第一题也是醉了,交了好多次才完全通过。😂😂😂

第一题:直接排序

#include <bits/stdc++.h>
using namespace std;
void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int &x : a) {
        cin >> x;
    }
    sort(a.begin(), a.end());
    if (k == 0) {
        cout << "YES\n";
        cout << 1 << '\n';
    } else {
        int ans = a[k - 1] + 1;
        int t = lower_bound(a.begin(), a.end(), ans) - a.begin();
        if (ans <= n && t == k) {
            cout << "YES\n";
            cout << ans << '\n';
        } else {
            cout << "NO\n";
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

第二题:简单的字符串处理

import sys


line = sys.stdin.readline().strip()
pre = "$"
ans = []
for c in line:
    if c != pre and c != " ":
        ans.append(c)
    if c != " ":
        pre = c
print("".join(ans))

第三题:有序集合(注意数据溢出)

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int n, x;
    long long ans = 0;
    set<int> S;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> x;
        auto it = S.lower_bound(x);
        if (it != S.begin()) {
            --it;
            ans += 1LL * i * (*it);
        }
        S.emplace(x);
    }
    cout << ans << '\n';
    return 0;
}

第四题:并查集

#include <bits/stdc++.h>
using namespace std;
#define N 100005
int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    vector<int> fa(N), size(N);
    for (int i = 0; i < N; ++i) {
        fa[i] = i;
        size[i] = 1;
    }
    function<int(int)> find_set = [&](int x) { return x == fa[x] ? x : (fa[x] = find_set(fa[x])); };
    auto union_sets = [&](int x, int y) -> bool {
        x = find_set(x), y = find_set(y);
        if (x == y) {
            return false;
        }
        if (size[x] < size[y]) {
            swap(x, y);
        }
        fa[y] = x;
        size[x] += size[y];
        return true;
    };
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &x : a) {
        cin >> x;
    }
    for (int i = 0; i < n / 2; ++i) {
        if (a[i] != a[i + n / 2]) {
            union_sets(a[i], a[i + n / 2]);
        }
    }
    unordered_map<int, vector<int>> mp;
    for (int i = 0; i < N; ++i) {
        mp[find_set(i)].emplace_back(i);
    }
    int ans = 0;
    for (auto it = mp.cbegin(); it != mp.cend(); ++it) {
        ans += it->second.size() - 1;
    }
    cout << ans << '\n';
    return 0;
}

第五题:二叉树中序遍历,直接模拟交换

#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left, *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int n, m, k, l, r, x;
    cin >> n >> m >> k;
    vector<TreeNode *> nodes(n + 1);
    for (int i = 1; i <= n; ++i) {
        nodes[i] = new TreeNode(i);
    }
    nodes[0] = nullptr;
    for (int i = 1; i <= n; ++i) {
        cin >> l >> r;
        nodes[i]->left = nodes[l];
        nodes[i]->right = nodes[r];
    }
    while (m--) {
        cin >> x;
        swap(nodes[x]->left, nodes[x]->right);
    }
    vector<int> ans;
    function<void(TreeNode *)> Dfs = [&](TreeNode *root) {
        if (!root) {
            return;
        }
        Dfs(root->left);
        ans.emplace_back(root->val);
        Dfs(root->right);
    };
    Dfs(nodes[k]);
    for (int i = 0; i < n; ++i) {
        cout << ans[i] << " \n"[i == n - 1];
    }
    return 0;
}


#美团笔试##笔试题目##美团#
全部评论
楼主有题干吗
1 回复 分享
发布于 2021-08-08 17:18
这api看起好简洁
点赞 回复 分享
发布于 2021-08-08 17:14
请问S.lower_bound(x)这个api有java类似的实现吗?
点赞 回复 分享
发布于 2021-08-15 19:18

相关推荐

会飞的猿:我看你想进大厂,我给你总结一下学习路线吧,java语言方面常规八股要熟,那些java的集合,重点背hashmap八股吧,jvm类加载机制,运行时分区,垃圾回收算法,垃圾回收器CMS、G1这些,各种乐观锁悲观锁,线程安全,threadlocal这些。在进阶一些的比如jvm参数,内存溢出泄漏排查,jvm调优。我这里说的只是冰山一角,详细八股可以去网上找,这不用去买,都免费资源。mysql、redis可以去看小林coding,我看你简历上写了,你一定要熟,什么底层b+树、索引结构、innodb、mvcc、undo log、redo log、行级锁表级锁,这些东西高频出现,如果面试官问我这些我都能笑出来。消息队列rabbitmq也好kafka也好,学一种就行,什么分区啊副本啊确认机制啊怎么保证不重复消费、怎么保证消息不丢失这些基本的一定要会,进阶一点的比如LEO、高水位线、kafka和rocketmq底层零拷贝的区别等等。计算机网络和操作系统既然你是科班应该理解起来问题不大,去看小林coding这两块吧,深度够了。spring boot的八股好好看看吧,一般字节腾讯不这么问,其他的java大厂挺爱问的,什么循环依赖啥的去网上看看。数据结构的话科班应该问题不大,多去力扣集中突击刷题吧。项目的话其实说白了还是结合八股来,想一想你写的这些技术会给你挖什么坑。除此之外,还有场景题、rpc、设计模式、linux命令、ddd等。不会的就别往简历上写了,虽然技术栈很多的话好看些,但背起来确实累。总结一下,多去实习吧,多跳槽,直到跳到一个不错的中厂做跳板,这是一条可行的进大厂的路线。另外,只想找个小厂的工作的话,没必要全都照这些准备,太累了,重点放在框架的使用和一些基础八股吧。大致路线就这样,没啥太多难度,就是量大,你能达到什么高度取决于你对自己多狠,祝好。
点赞 评论 收藏
分享
评论
4
25
分享

创作者周榜

更多
牛客网
牛客企业服务