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树。

  • 相关阅读:
    近期前端中的 一些常见的面试题
    一道前端学习题
    前端程序员容易忽视的一些基础知识
    web前端工程师入门须知
    Web前端知识体系精简
    面试分享:一年经验初探阿里巴巴前端社招
    抽象类、抽象函数/抽象方法详解
    C#语法-虚方法详解 Virtual 虚函数
    面向对象语言:继承关系教程
    C#动态创建Xml-LinQ方式
  • 原文地址:https://www.cnblogs.com/lovers/p/4803636.html
Copyright © 2011-2022 走看看