算法笔试面试1
Posted trevo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法笔试面试1相关的知识,希望对你有一定的参考价值。
蛇形矩阵
题目链接: https://www.acwing.com/activity/content/problem/content/1898/1/
思路
设计一个偏移量,4个方向,走不通了就改变方向
实现
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100;
int n, m;
int res[N][N];
int main()
{
cin >> n >> m;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
for(int x = 0, y = 0, k = 1, d = 0; k <= n * m; k++)
{
res[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if(a < 0 || a >= n || b < 0 || b >= m || res[a][b])
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
cout << res[i][j] << " ";
cout << endl;
}
return 0;
}
单链表快速排序
题目链接:https://www.acwing.com/activity/content/problem/content/1899/1/
思路
创建三个指针,left,mid,right,分别是比基准小,等于基准,大于基准的数字,递归在对left,right两个链表进行排序
最后将三个链表拼接起来即可,记得最后释放空间。
链表的快速排序算法是稳定排序。
实现
/**
* 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 < val) ltail = ltail -> 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;
}
};
链表的归并排序
迭代
递归
寻找矩阵的极小值
题目链接:https://www.acwing.com/activity/content/problem/content/1900/1/
思路
leetcode peek 162
实现
// Forward declaration of queryAPI.
// int query(int x, int y);
// return int means matrix[x][y].
class Solution {
public:
vector<int> getMinimumValue(int n) {
typedef long long LL;
const LL INF = 1e15;
int l = 0, r = n - 1;
while(l < r)
{
int mid = (l + r) >> 1; //中点
int k;
LL val = INF;
//求这一列的最小值
for(int i = 0; i < n; i++)
{
int t = query(i, mid);
if(t < val)
{
val = t;
k = i;
}
}
LL left = mid ? query(k, mid - 1) : INF;
LL right = mid + 1 < n ? query(k, mid + 1) : INF;
if(val < left && val < right) return {k, mid};
if(left < val) r = mid - 1;
else l = mid + 1;
}
int k;
LL val = INF;
for(int i = 0; i < n; i++)
{
int t = query(i, r);
if(t < val)
{
val = t;
k = i;
}
}
return {k, r};
}
};
鸡蛋的硬度
题目链接:https://www.acwing.com/activity/content/problem/content/1901/1/
思路
实现
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110, M = 11;
int n,m;
int f[N][M];
int main()
{
while(cin >> n >> m)
{
for(int i = 1; i <= n; i++) f[i][1] = i;
for(int i = 1; i <= m; i++) f[1][i] = 1;
for(int i = 2; i <= n; i++)
for(int j = 2; j <= m; j++)
{
f[i][j] = f[i][j - 1];
for(int k = 1; k <= i; k++)
f[i][j] = min(f[i][j], max(f[k - 1][j - 1], f[i - k][j]) + 1);
}
cout << f[n][m] << endl;
}
return 0;
}
以上是关于算法笔试面试1的主要内容,如果未能解决你的问题,请参考以下文章