寒武纪面试 测试开发

一面:

2020.07.29

链表相交,

实现memcpy函数,

newmalloc的区别

 

memcpy函数的实现:

void *memcpy(void *pDest, const void*pSrc, unsigned int n) {

assert((NULL != pDest)&&(NULL != pSrc))

char *pTmpDest = (char*)pDest;

char *pTmpSrc = (char*)pSrc;

while(n--) {

*pTmpDest = *pTmpSrc;

pTmpDest++;

pTmpSrc++;

}

 

return pDest;

}

 

void* upgrade_memcpy(void* pDest, const void* pSrc, size_t n) {

assert((pDest!=NULL)&&(pSrc!=NULL))

int wordnum = n/4;

int slice = n%4;

int * pIntsrc =  (int*)pSrc;

int * pIntdest = (int*)pDest;

 

while(wordnum--) 
*pIntdest++ = *Intsrc++;

while(slice--)

    *((char*)pIntdest)++ = *((char*)pIntsrc)++;

 

return pDest;

}

 

二面

2020.08.05

1、多线程

2、IPC、共享内存

3、bind

4、合并n个有序链表 (力扣原题 使用最小堆会快一些)

 

#include <queue>

using namespace std;

 

struct ListNode {

    int val;

    ListNode* next;

    ListNode(int x) : val(x), next(NULL) {}

};

 

class Solution {

    struct cmp {

        bool operator() (ListNode* a, ListNode* b) {

            return a->val > b->val;

        }

    };

    

    ListNode* mergeKLists(vector<ListNode*>& lists) {

        priority_queue<ListNode*, vector<ListNode*>, cmp> pri;

        for (auto &elem : lists) {

            if (elem) {

                pri.push(elem);

            }

        }

        

        ListNode* head = new ListNode(0);

        ListNode* pre = head;

        while(!pri.empty()) {

            pre->next = pri.top();

            ListNode* tmp = pri.top()->next;

            pri.pop();

            if (tmp) {

                pri.push(tmp);

            }

            pre = pre->next;

        }

        

        pre = head->next;

        delete head;

        

        return pre;

    };

};

posted @   小海哥哥de  阅读(440)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示