zoukankan      html  css  js  c++  java
  • 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 << "	Expected ret=" << tc.ret << endl;
                cout << "	Acutal   ret=" << actual_ret << endl;
            }
    
            actual_ret = find2(tc.nums);
    
            if (tc.ret != actual_ret) {
                cout << "Case #" << iii << ": FAILED" <<  endl;
                cout << "	Expected ret=" << tc.ret << endl;
                cout << "	Acutal   ret=" << actual_ret << endl;
            }
    
        }
    
        return 0;
    }

    3. 问github上的项目,推荐TypoChecker使用Trie树。

  • 相关阅读:
    POJ3213(矩阵乘法)
    jquery:ajax不接收返回值回
    Swift UI学习UITableView and protocol use
    也可以看看GCD(杭州电2504)(gcd)
    数据结构—队列
    HDU 4946 Area of Mushroom 凸包
    UVa 353
    照片教你eclipse通过使用gradle 打包Android
    普林斯顿大学公开课 算法1-8:并检查集合 高速查找
    Codeforces Round #246 (Div. 2)
  • 原文地址:https://www.cnblogs.com/lovers/p/4803636.html
Copyright © 2011-2022 走看看