映客直播 2020/7/22 笔试算法题(已offer直通卡
比较简单都A了
1. 给定一个数组,每个数组的元素代表从这个位置能够往右跳跃几步,问从第一步能否跳到最后一步?
思路:
暴力:遍历能够达到的最远位置
贪心,遍历每个元素,如果当前位置<=能够达到的最远位置 则 更新能够达到的最远位置(当前位置+元素值 > 最远位置)。如果最远位置大于等于最大长度可以提前返回
2. 第二题既然和第一题一摸一样。。。。。
3. 给定一个链表,奇数偶数位置进行拆分并且返回单链表,奇数在前偶数在后,空间复杂度应该为1。
先拆分再合并
4. 给定一个字符串,一个step,问右循环移动step步后的新字符串。
前strlen-step进行中心对换,后step进行中心对换,整体进行中心对换
char* LoopMove(char* pStr, int steps) {
// write code here
int strLen = 0;
for (; pStr[strLen] != ''; strLen++){
}
// 求mod
steps = steps % strLen;
// 前后半段 反转
// 前半段
// 开始
int low = 0;
int high = strLen - steps-1;
while (low < high){
// 交换
char temp = pStr[low];
pStr[low] = pStr[high];
pStr[high] = temp;
low++;
high--;
}
//后半段
low = strLen - steps;
high = strLen - 1;
while (low < high){
// 交换
char temp = pStr[low];
pStr[low] = pStr[high];
pStr[high] = temp;
low++;
high--;
}
// 总体交换
low = 0;
high = strLen - 1;
while (low < high){
// 交换
char temp = pStr[low];
pStr[low] = pStr[high];
pStr[high] = temp;
low++;
high--;
}
return pStr;
}
查看15道真题和解析