zoukankan      html  css  js  c++  java
  • 2015-08-13 [百度]--实习开发测试工程师--1面

    时间:2015-08-13  16:40 ~ 18:20

    地点:北京市海淀区西北旺东路10号院  百度科技园1号楼

    1. 问简历

    。。。

    2. 算法

    只问了一道算法题目。

    leetcode原题稍微改了下。

    https://leetcode.com/problems/copy-list-with-random-pointer/

    该题目的解法在这里 : https://github.com/loverszhaokai/leetcode/blob/master/138/sol_1S_NT.cc

    题目:在线性链表的基础上,每一个节点都增加一个random指针,该random指针指向该节点或者该节点之后的节点。返回深拷贝。

    解法1:

    struct RandomListNode {
        int label;
        RandomListNode *next, *random;
        RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
    };
    
    /**
     * Definition for singly-linked list with a random pointer.
     * struct RandomListNode {
     *     int label;
     *     RandomListNode *next, *random;
     *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
     * };
     */
    class Solution {
    public:
        // Time  Complexcity: O(n^2)
        // Space Complexcity: O(n)
        RandomListNode *copyRandomList(RandomListNode *head) {
            list<RandomListNode *> random_nodes;
    
            if (!head)
                return NULL;
    
            RandomListNode *new_head = new RandomListNode(head->label);
            if (head->random == head) {
                new_head->random = new_head;
            } else {
                new_head->random = head->random;
                random_nodes.push_back(new_head->random);
            }
    
            RandomListNode *old_node = head->next;
            RandomListNode *new_node;
            RandomListNode *pre_node = new_head;
    
            while (old_node) {
                new_node = new RandomListNode(old_node->label);
                pre_node->next = new_node;
                pre_node = new_node;
    
                // Traverse random_nodes to find all the nodes whose random point to this node
                list<RandomListNode *>::iterator it = random_nodes.begin();
                while (it != random_nodes.end()) {
                    if (*it == old_node) {
                        *it = new_node;
                        random_nodes.erase(it);
                    } else 
                        it++;
                }
    
                if (old_node->random == old_node) {
                    new_node->random = new_node;
                } else {
                    new_node->random = old_node->random;
                    random_nodes.push_back(new_node->random);
                }
    
                old_node = old_node->next;
            }
         return new_head; } };

    解法2:

    /**
     * Definition for singly-linked list with a random pointer.
     * struct RandomListNode {
     *     int label;
     *     RandomListNode *next, *random;
     *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
     * };
     */
    class Solution {
    public:
        // Time  Complexcity: O(n)
        // Space Complexcity: O(n)
        RandomListNode *copyRandomList(RandomListNode *head) {
            unordered_map<RandomListNode *, list<RandomListNode *> > parent_nodes;
    
            if (!head)
                return NULL;
    
            RandomListNode *new_head = new RandomListNode(head->label);
            if (head->random == head) {
                new_head->random = new_head;
            } else {
                parent_nodes[head->random].push_back(new_head);
            }
    
            RandomListNode *old_node = head->next;
            RandomListNode *new_node;
            RandomListNode *pre_node = new_head;
    
            while (old_node) {
                new_node = new RandomListNode(old_node->label);
                pre_node->next = new_node;
                pre_node = new_node;
    
                // Update the random pointer
                list<RandomListNode *> nodes = parent_nodes[old_node];
                list<RandomListNode *>::iterator it = nodes.begin();
                while (it != nodes.end()) {
                    it->random = new_node;
                    it++;
                }
    
                if (old_node->random == old_node) {
                    new_node->random = new_node;
                } else {
                    parent_nodes[old_node->random].push_back(new_node);
                }
    
                old_node = old_node->next;
            }
    return new_head; } };

    解法3:[面试官提示使用原来的链表,以达到O(1)的存储空间]  该方法不适合leetcode上的138题

    struct RandomListNode {
        int label;
        RandomListNode *next, *random;
        RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
    };
    
    /**
     * Definition for singly-linked list with a random pointer.
     * struct RandomListNode {
     *     int label;
     *     RandomListNode *next, *random;
     *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
     * };
     */
    class Solution {
    public:
        // Time  Complexcity: O(n)
        // Space Complexcity: O(1)
        RandomListNode *copyRandomList(RandomListNode *head) {
            if (!head)
                return NULL;
    
            RandomListNode *new_head = new RandomListNode(head->label);
            RandomListNode *old_node = head;
            RandomListNode *new_node = new_head;
            RandomListNode *pre_node = new_head;
            RandomListNode *next_old_node;
    
            new_node->random = old_node;
            next_old_node = old_node->next;
            old_node->next = new_node;
            old_node = next_old_node;
    
            while (old_node) {
                new_node = new RandomListNode(old_node->label);
                pre_node->next = new_node;
                pre_node = new_node;
    
                new_node->random = old_node;
                next_old_node = old_node->next;
                old_node->next = new_node;
                old_node = next_old_node;
            }
    
            old_node = head;
            new_node = new_head;
    
            while (new_node) {
                old_node = new_node->random;
                new_node->random = old_node->random == NULL ? NULL : (old_node->random)->next;
                old_node->next = new_node->next == NULL ? NULL : (new_node->next)->random;
    
                new_node = new_node->next;
            }
            return new_head;
        }
    };

    3. 面试的实习职位,不会发校招offer,应该是88了。

  • 相关阅读:
    缺失的第一个正数
    tuple用法
    整数转罗马数字
    三种时间格式的转换
    不同包的调用
    正则表达式
    lgb模板
    线性回归
    时间序列的特征
    3D聚类
  • 原文地址:https://www.cnblogs.com/lovers/p/4728391.html
Copyright © 2011-2022 走看看