【算法】笔试面试1
2021-01-27 19:13
标签:mini ble code 最小 win ace href 空间 space 题目链接: https://www.acwing.com/activity/content/problem/content/1898/1/ 设计一个偏移量,4个方向,走不通了就改变方向 题目链接:https://www.acwing.com/activity/content/problem/content/1899/1/ 创建三个指针,left,mid,right,分别是比基准小,等于基准,大于基准的数字,递归在对left,right两个链表进行排序 题目链接:https://www.acwing.com/activity/content/problem/content/1900/1/ leetcode peek 162 题目链接:https://www.acwing.com/activity/content/problem/content/1901/1/ 【算法】笔试面试1 标签:mini ble code 最小 win ace href 空间 space 原文地址:https://www.cnblogs.com/Trevo/p/12841709.html蛇形矩阵
思路
实现
#include
单链表快速排序
思路
最后将三个链表拼接起来即可,记得最后释放空间。
链表的快速排序算法是稳定排序。实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//得到尾节点
ListNode* get_tail(ListNode* head){
while(head -> next) head = head -> next;
return head;
}
ListNode* quickSortList(ListNode* head) {
if(!head || !head->next) return head;
auto left = new ListNode(-1), mid = new ListNode(-1), right = new ListNode(-1);
auto ltail = left, mtail = mid, rtail = right;
int val = head -> val;
for(auto p = head; p; p = p -> next)
{
if(p -> val next = p;
else if(p -> val == val) mtail = mtail -> next = p;
else rtail = rtail -> next = p;
}
ltail -> next = mtail -> next = rtail -> next = NULL;
left -> next = quickSortList(left -> next);
right -> next = quickSortList(right -> next);
//拼接三个链表
get_tail(left) -> next = mid -> next;
get_tail(left) -> next = right -> next;
auto p = left -> next;
delete left;
delete mid;
delete right;
return p;
}
};
链表的归并排序
迭代
递归
寻找矩阵的极小值
思路
实现
// Forward declaration of queryAPI.
// int query(int x, int y);
// return int means matrix[x][y].
class Solution {
public:
vector
鸡蛋的硬度
思路
实现
#include