微软笔试8.26分享
欢迎互对答案~
// 第1题 int solution(string &S) { unordered_map<char, int> m; m[0] = -1; int n = S.size(); int res = 0; int cur = 0; for (int i = 0; i < n; ++i) { cur ^= (1 << (S[i] - 'a')); if (m.count(cur)) res = max(res, i - m[cur]); else m[cur] = i; } return res; } // 第2题 int solution(vector<int> &A, int M) { // write your code in C++ (C++14 (g++ 6.2.0)) unordered_map<int, int> m; int res = 0; for (const int &num : A) { int val = num >= 0 ? num : (num % M) + M; ++m[val % M]; res = max(res, m[val % M]); } return res; } // 第三题 int solution(vector<int> &A, vector<int> &B) { // write your code in C++ (C++14 (g++ 6.2.0)) unordered_set<int> s; int n = A.size(); for (int i = 0; i < n; ++i) { if (A[i] == B[i]) s.emplace(A[i]); } for (int i = 0; i < n; ++i) { if (s.count(A[i]) || s.count(B[i])) continue; s.emplace(max(A[i], B[i])); } int res = 1; while (s.count(res)) { ++res; } return res; }