我又回来更新啦💃
一面
笔试
// 在线面试平台。将链接分享给你的朋友以加入相同的房间。
// Author: tdzl2003<dengyun@meideng.net>
1.实现内存拷贝函数,将src地址中长度为len的数据拷贝到dst指向的内存
int memcpy(void* src, void* dst, int len)
{
for (int i = 0; i < len; i++) {
dst[i] = src[i];
}
}
2.
以给定值x为基准对单链表排序,所有小于x的结点排在大于或等于x的结点之前.
请返回重新排列后的链表的头指针。注意:要求稳定排序
ListNode* sort(ListNode* pHead, int x) {
ListNode* smaller, bigger;
ListNode* smallerp, biggerp;
ListNode* p = pHead;
int cnt = 0;
while (p != null) {
if (p -> val < x || (p -> val == x && cnt < x)) {
if (smaller == null) {
smaller = p;
smallerp = smaller;
}
else {
smallerp -> next = p;
smallerp = smaller -> next;
}
}
if (p -> val > x || (p -> val == x && cnt > x)) {
if (bigger == null) {
bigger = p;
biggerp = bigger;
}
else {
biggerp -> next = p;
biggerp = bigger -> next;
}
}
p = p -> next; cnt++;
}
if (smaller == null)
smaller = bigger;
smallerp -> next = bigger;
return smaller;
}
3.
判断二叉树是否为搜索二叉树
bool smaller(TreeNode* p, int val) {
if (p == null)
return true;
return p -> val < val && smaller(p -> left, val) && smaller(p -> right, val);
}
bool bigger(TreeNode* p, int val) {
if (p == null)
return true;
return p -> val > val && bigger(p -> left, val) && bigger(p -> right, val);
}
bool isTrue(TreeNode* root) {
if (root == null)
return true;
return smaller(root -> left, root -> val) && bigger(root -> right, root -> val);
}
4.给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。
例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5};
vector<int> maxInWindows(const vector<int>& num, unsigned int size) {
vector<int> res;
int max_num = num[0], max_id = 0;
for (int i = 1; i < size; i++) {
if (num[i] > max_num) {
max_id = i;
max_num = num[i];
}
}
res.push_back(max_num);
for (int l = 1, r = l + size; r < num.length(); l++, r++) {
if (l - 1 == max_id) {
max_id = l;
max_num = num[l];
for (int j = l; j < r; j++) {
if (num[i] > max_num) {
max_id = i;
max_num = num[i];
}
}
}
if (num[r-1] > max_num) {
max_id = r - 1;
max_num = num[r-1];
}
res.push_back(max_num);
}
return res;
}
前三题多多少少有点问题,请勿参考。
面试
问的问题忘了
讲了下之前在阿里的实习
结果:挂了