注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
已经挂了 被面试官怀疑见过原题 发上来我准备的面经合集 为后面的onsite攒攒人品
#include<queue> #include<iostream> #include<mutex> using namespace std;
class HitCounter{ private: mutex l; queue<int> q; public: HitCounter(){} void hit(int t) { l.lock(); q.push(t); l.unlock(); } int getHit(int timestamp) { l.lock(); while(!q.empty() && timestamp - q.front() >= 300) { q.pop(); } l.unlock(); return q.size(); } };
int main() { HitCounter h1; h1.hit(1); h1.hit(2); h1.hit(3); h1.hit(300); cout << h1.getHit(300) << endl; // h1.hit(301); cout << h1.getHit(301) << endl; } ..
2. word pattern
class Solution { public: bool wordPattern(string pattern, string str) { stringstream ss(str); string s; int count = 0; unordered_map<string, char> map1; unordered_map<char, string> map2; while(ss >> s) { if(count >= pattern.size()) return false; if(map1.find(s) == map1.end()) { map1 = pattern[count]; } else { if(map1 != pattern[count]) return false; } if(map2.find(pattern[count]) == map2.end()) { map2[pattern[count]] = s; } else{ if(map2[pattern[count]] != s) return false; } count++; } if(count < pattern.size()) return false; else return true; int interval = min(1000, size - start); unordered_map<int, vector<string>> m; if(start >= size) return {value}; for(int i = 0; i < value.size(); i++) { int hash_key = get_hash(value[i], start, interval); m[hash_key].push_back(value[i]); } if(m.size() == value.size()) return res; for(auto t = m.begin(); t != m.end(); t++) { if(t->second.size() > 1) { vector<vector<string>> vec = find_same_file(it->second, start + interval, size); res.insert(res.end(), vec.begin(), vec.end()); } } return res; } vector<vector<string>> find_dul_files(string input) { vector<string> files = find_all_files(input); vector<vector<string>> res; unordered_map<int, vector<string>> map; for(int i = 0; i < files.size(); i++) { map[get_size(files[i])].push_back(files[i]) } for(auto it = map.begin(); it != map.end(); it++) { if((it->second).size() > 1) { vector<vector<string>> tmp_res = find_same_file(it->second, 0, it->first); res.insert(res.end(), tmp_res.begin(), tmp_res.end()); } } return res; }
|