【算法】笔试面试1

2021-01-27 19:13

阅读:611

标签:mini   ble   code   最小   win   ace   href   空间   space   

蛇形矩阵

题目链接: https://www.acwing.com/activity/content/problem/content/1898/1/

思路

设计一个偏移量,4个方向,走不通了就改变方向

实现

#include 
#include 
#include 
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 || 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 

单链表快速排序

题目链接: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  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 getMinimumValue(int n) {
        typedef long long LL;
        const LL INF = 1e15;
        
        int l = 0, r = n - 1;
        while(l > 1; //中点
            
            int k;
            LL val = INF;
            //求这一列的最小值
            for(int i = 0; i 

鸡蛋的硬度

题目链接:https://www.acwing.com/activity/content/problem/content/1901/1/

思路

技术图片

实现

#include 
#include 
#include 
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 

【算法】笔试面试1

标签:mini   ble   code   最小   win   ace   href   空间   space   

原文地址:https://www.cnblogs.com/Trevo/p/12841709.html


评论


亲,登录后才可以留言!