2015-09-09 [一点资讯]--数据抓取和处理工程师--4面
时间:2015-09-09 14:40 ~ 15:40
地点:北京市海淀区王庄路1号 清华同方科技广场D座 西区 7层
1. 问项目经验和看过什么书
2. 有3N+1个整数,其中只有一个数出现了一次,其它的数都出现了3次,查找唯一出现一次的那个数。
https://leetcode.com/problems/single-number-ii/
https://github.com/loverszhaokai/leetcode/blob/master/137_no/sol_bit_manipulation.cc
#include <climits> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <vector> using namespace std; int find(const vector<int> &nums) { int count[32]; for (int iii = 0; iii < sizeof(count) / sizeof(int); iii++) count[iii] = 0; for (int iii = 0; iii < nums.size(); iii++) { int jjj = 1; int kkk = 1; while (jjj <= 32) { if (nums[iii] & kkk) count[jjj - 1] = (count[jjj - 1] + 1) % 3; kkk <<= 1; jjj++; } } int ans = 0; int jjj = 1; for (int iii = 0; iii < sizeof(count) / sizeof(int); iii++) { if (count[iii]) ans |= jjj; jjj <<= 1; } return ans; } // Use two integer instead of count[32] // 0 0 -> 0 // 0 1 -> 1 // 1 0 -> 2 // int find2(const vector<int> &nums) { int high = 0; int low = 0; for (int iii = 0; iii < nums.size(); iii++) { int jjj = 1; int kkk = 1; while (jjj <= 32) { if (nums[iii] & kkk) { if ((high & kkk) ^ (low & kkk) ^ kkk) low |= kkk; else low &= ~kkk; if ((high & kkk) ^ (low & kkk) ^ kkk) high |= kkk; else high &= ~kkk; } kkk <<= 1; jjj++; } } int ans = 0; int iii = 1; int jjj = 1; while (iii <= 32) { if (low & jjj || high & jjj) ans |= jjj; jjj <<= 1; iii++; } return ans; } int main() { const struct TestCase { vector<int> nums; int ret; } test_cases[] = { { { 1, 1, 1, 3 }, 3 }, { { 1, 1, 1, 2, 3, 2, 2 }, 3 }, { { 1, 1, 1, 2, 3, 2, 2, 3, 4, 3 }, 4 }, }; for (int iii = 0; iii < sizeof(test_cases) / sizeof(TestCase); iii++) { const TestCase &tc = test_cases[iii]; int actual_ret = find(tc.nums); if (tc.ret != actual_ret) { cout << "Case #" << iii << ": FAILED" << endl; cout << "\tExpected ret=" << tc.ret << endl; cout << "\tAcutal ret=" << actual_ret << endl; } actual_ret = find2(tc.nums); if (tc.ret != actual_ret) { cout << "Case #" << iii << ": FAILED" << endl; cout << "\tExpected ret=" << tc.ret << endl; cout << "\tAcutal ret=" << actual_ret << endl; } } return 0; }
3. 问github上的项目,推荐TypoChecker使用Trie树。
作者:loverszhaokai
出处:http://www.cnblogs.com/lovers
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。