备战秋招(coding篇)

时间:2024-04-01 18:16:49

其中coding题目来源于师兄面试经验

目录

1、链表的结构体+反转链表

2、实现一个网页类 能够进行前进、后退、跳转操作

3、二维矩阵,由0、1填充,其中1代表陆地,0代表海洋,找出距离陆地最远的那块海洋的位置

4、现有一个能产生随机数字1-5的函数,请写出一个能产生随机数字1-7的函数

5、写出树的结构,并逐层打印二叉树

6、路上有很多车,每个车由一个区间表示,将小区间合并成大区间,并返回。考察面向对对象的编程,将区间用结构体表示。注意编程规范,排序算法中lamada表达式的应用。

7、冒泡排序

8、二维表格,每步移动一下,只能向下和向右,中间有障碍物,从左上角到右下角,有多少种走法。

9、二维矩阵左上角到右下角,求数字和最小路径

10、蛇形矩阵

11、飞机上一个乘客忘记带票,随机坐了一个位置,最后一个乘客坐原来位置的概率?

​​​​​​​12、最大子序和

1、链表的结构体+反转链表

本质上就是一个构造函数

struct ListNode{
  int val_;
  ListNode* next_;
  ListNode() : val_(0), next_(NULL) {}
  ListNode(int x) : val_(x), next_(NULL) {}
  ListNode(int x, ListNode* next) : val_(x), next_(next) {}
};

反转链表:

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
/*
struct ListNode{
  int val_;
  ListNode* next_;
  ListNode() : val_(0), next_(NULL) {}
  ListNode(int x) : val_(x), next_(NULL) {}
  ListNode(int x, ListNode* next) : val_(x), next_(next) {}
};
*/
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;
        while(cur) {
            ListNode* tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};

2、实现一个网页类 能够进行前进、后退、跳转操作

#include <iostream>
#include <stack>
#include <string>
using namespace std; // 这里为了方便开发

class Web {
public:
    Web() {};

    // 访问
    void Visit(const string &pages) {
        while (!backed_pages_.empty()) {
            backed_pages_.pop();
        }
        visited_pages_.push(pages);
        cout << pages << endl;
    }

    // 后退
    void Back() {
        if (visited_pages_.empty()) {
            cout << "ingore" << endl;
        } else {
            backed_pages_.push(visited_pages_.top());
            visited_pages_.pop();
            if (!visited_pages_.empty()) {
                cout << visited_pages_.top() << endl;
            } else {
                cout << "ingore" << endl;
                visited_pages_.push(backed_pages_.top());
                backed_pages_.pop();
            }
        }
    }

    // 前进
    void Forward() {
        if (backed_pages_.empty()) {
            cout << "ingore" << endl;
        } else {
            visited_pages_.push(backed_pages_.top());
            backed_pages_.pop();
            cout << visited_pages_.top() << endl;
        }
    }
    ~Web() {};

private:
    stack<string> visited_pages_; // 记录访问的页面
    stack<string> backed_pages_;  // 记录回退的页面
};

int main() {
    int n;
    cin >> n;
    Web web;
    for (int i = 0; i < n; ++i){
        string action, page;
        cin >> action;
        if (action == "VISIT") {
            cin >> page;
            web.Visit(page);
        } else if (action == "BACK") {
            web.Back();
        } else if (action == "FORWARD") {
            web.Forward();
        } else {
            cout << "Invalid Action!" << endl;
        }
    }
    return 0;
}
8
VISIT
www.baidu.com
www.baidu.com
VISIT
www.sougou.com
www.sougou.com
BACK
www.baidu.com
FORWARD
www.sougou.com

3、二维矩阵,由0、1填充,其中1代表陆地,0代表海洋,找出距离陆地最远的那块海洋的位置

对应力扣1162题

你现在手里有一份大小为 n x n 的 网格 grid,上面的每个 单元格 都用 0 和 1 标记好了。其中 0 代表海洋,1 代表陆地。

请你找出一个海洋单元格,这个海洋单元格到离它最近的陆地单元格的距离是最大的,并返回该距离。如果网格上只有陆地或者海洋,请返回 -1

我们这里说的距离是「曼哈顿距离」( Manhattan Distance):(x0, y0) 和 (x1, y1) 这两个单元格之间的距离是 |x0 - x1| + |y0 - y1| 。

示例 1:

输入:grid = [[1,0,1],[0,0,0],[1,0,1]]
输出:2
解释: 
海洋单元格 (1, 1) 和所有陆地单元格之间的距离都达到最大,最大距离为 2。
class Solution {
public:
    static constexpr int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    static constexpr int MAX_N = 100 + 5;

    struct Coordinate {
        int x, y, step;
    };

    int n, m;
    vector<vector<int>> a;

    bool vis[MAX_N][MAX_N];

    int findNearestLand(int x, int y) {
        memset(vis, 0, sizeof vis);
        queue <Coordinate> q;
        q.push({x, y, 0});
        vis[x][y] = 1;
        while (!q.empty()) {
            auto f = q.front(); q.pop();
            for (int i = 0; i < 4; ++i) {
                int nx = f.x + dx[i], ny = f.y + dy[i];
                if (!(nx >= 0 && nx <= n - 1 && ny >= 0 && ny <= m - 1)) {
                    continue;
                }
                if (!vis[nx][ny]) {
                    q.push({nx, ny, f.step + 1});
                    vis[nx][ny] = 1;
                    if (a[nx][ny]) {
                        return f.step + 1;
                    }
                }
            }
        }
        return -1;
    }
    
    int maxDistance(vector<vector<int>>& grid) {
        this->n = grid.size();
        this->m = grid.at(0).size();
        a = grid;
        int ans = -1;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (!a[i][j]) {
                    ans = max(ans, findNearestLand(i, j));
                }
            }
        }
        return ans;
    }
};

这道题目先贴这里,最近还没看图论,TODO

4、现有一个能产生随机数字1-5的函数,请写出一个能产生随机数字1-7的函数

#include<iostream>
#include<ctime>
#include<cstdlib>

int GenerateRandom5() {
    return (rand() % 5) + 1;
}

int GenerateRandom7() {
    int x = ~(1<<31);
    while(x > 21) {
        x = 5 * (GenerateRandom5() - 1) + GenerateRandom5();
    }
    return x % 7 + 1;
}

int main() {
    srand(static_cast<unsigned int>(time(0)));  
    // 生成并输出一个1到7之间的随机数  
    int randomNumber = GenerateRandom7();  
    std::cout << "Random number between 1 and 7: " << randomNumber << std::endl;  
    return 0;  
}

5、写出树的结构,并逐层打印二叉树

struct TreeNode {
  int val_;
  TreeNode* left_;
  TreeNode* right_;
  TreeNode() : val_(0), left_(nullptr), right_(nullptr) {};
  TreeNode(int val) : val_(val), left_(nullptr), right_(nullptr) {};
  TreeNode(int val, TreeNode* left, TreeNode* right) 
             : val_(val), left_(left), right_(right) {};
};
#include<iostream>
#include<vector>
#include<queue>

using namespace std;

struct TreeNode {
  int val;
  TreeNode* left;
  TreeNode* right;
  TreeNode() : val(0), left(nullptr), right(nullptr) {};
  TreeNode(int val) : val(val), left(nullptr), right(nullptr) {};
  TreeNode(int val, TreeNode* left, TreeNode* right) 
             : val(val), left(left), right(right) {};
};

void PrintLevelOrder(TreeNode* root) {
    queue<TreeNode*> que;
    if(root) que.push(root);
    while(!que.empty()) {
        int size = que.size();
        vector<int> tmp;
        for(int i = 0; i < size; i++) {
            TreeNode* cur = que.front();
            que.pop();
            cout << cur->val << "";
            if(cur->left) que.push(cur->left);
            if(cur->right) que.push(cur->right);
        }
        cout << endl;
    }
}

int main() {
    TreeNode* root = new TreeNode(1);  
    root->left = new TreeNode(2);  
    root->right = new TreeNode(3);  
    root->left->left = new TreeNode(4);  
    root->left->right = new TreeNode(5);  
    root->right->left = new TreeNode(6);  
    root->right->right = new TreeNode(7);  
    PrintLevelOrder(root);
    return 0;
}
1
23
4567

6、路上有很多车,每个车由一个区间表示,将小区间合并成大区间,并返回。考察面向对对象的编程,将区间用结构体表示。注意编程规范,排序算法中lamada表达式的应用。

#include<iostream>
#include<vector>

using namespace std;

class Interval{
public:
    int start;
    int end;
    Interval() : start(0), end(0) {};
    Interval(int s, int e) : start(s), end(e) {}; 

    void print() const {  
        cout << "[" << start << ", " << end << "]";  
    }  
};

vector<Interval> MergeInterval(vector<Interval>& intervals) {
    if(intervals.size() < 2) return intervals;

    sort(intervals.begin(), intervals.end(), 
          [](const Interval& a, const Interval& b) {
        return a.start < b.start;
    });

    std::vector<Interval> merged;  
    Interval current = intervals[0];  
    for (size_t i = 1; i < intervals.size(); ++i) {  
        if (current.end < intervals[i].start) {  
            merged.emplace_back(current);  
            current = intervals[i];  
        } else {  
            current.end = max(current.end, intervals[i].end);  
        }  
    } 
    //最后一个区间的也需要push进来
    merged.emplace_back(current);  
    return merged;  
}

int main() {  
    std::vector<Interval> intervals = {Interval(1, 3), 
            Interval(2, 6), Interval(8, 10), Interval(15, 18)};  
    std::vector<Interval> mergedIntervals = MergeInterval(intervals);  
    for (const auto& interval : mergedIntervals) {  
        interval.print();  
        std::cout << " ";  
    }  
    std::cout << std::endl;  
    return 0;  
}
[1, 6] [8, 10] [15, 18]

7、冒泡排序

#include<iostream> 
#include<vector>

using namespace std;

void BubbleSort(vector<int>& nums) {
    for(int i = 0; i < nums.size(); i++) {
        //提前退出标志符号
        bool flag = false;
        for(int j = 0; j < nums.size() - i - 1; j++) {
            if(nums[j] > nums[j + 1]) {
                swap(nums[j], nums[j + 1]);
                flag = true;
            }
        }
        if(!flag) break;
    }
}

int main() {
    vector<int> nums = {6, 5, 4, 3, 2, 1};
    BubbleSort(nums);
    for(const auto& num : nums) {
        cout << num << " ";
    }
    return 0;
}
1 2 3 4 5 6

8、二维表格,每步移动一下,只能向下和向右,中间有障碍物,从左上角到右下角,有多少种走法。

9、二维矩阵左上角到右下角,求数字和最小路径

10、蛇形矩阵

#include <iostream>  
#include <vector>  
  
using namespace std;  
  
vector<vector<int>> generateSnakeMatrix(int n) {  
    vector<vector<int>> matrix(n, vector<int>(n));  
    int num = 1;  
    int left = 0, right = n - 1, top = 0, bottom = n - 1;  
    int direction = 0;  // 0: right, 1: down, 2: left, 3: up  
  
    while (left <= right && top <= bottom) {  
        if (direction == 0) {  
            for (int i = left; i <= right; ++i) {  
                matrix[top][i] = num++;  
            }  
            ++top;  
        } else if (direction == 1) {  
            for (int i = top; i <= bottom; ++i) {  
                matrix[i][right] = num++;  
            }  
            --right;  
        } else if (direction == 2) {  
            for (int i = right; i >= left; --i) {  
                matrix[bottom][i] = num++;  
            }  
            --bottom;  
        } else if (direction == 3) {  
            for (int i = bottom; i >= top; --i) {  
                matrix[i][left] = num++;  
            }  
            ++left;  
        }  
        direction = (direction + 1) % 4;  
    }   
    return matrix;  
}  
  
int main() {  
    int n;  
    cout << "Enter the size of the snake matrix: ";  
    cin >> n;  
    vector<vector<int>> matrix = generateSnakeMatrix(n);  
    for (const auto& row : matrix) {  
        for (int num : row) {  
            cout << num << " ";  
        }  
        cout << endl;  
    }  
    return 0;  
}
Enter the size of the snake matrix: 6
1 2 3 4 5 6 
20 21 22 23 24 7 
19 32 33 34 25 8 
18 31 36 35 26 9 
17 30 29 28 27 10 
16 15 14 13 12 11 

11、飞机上一个乘客忘记带票,随机坐了一个位置,最后一个乘客坐原来位置的概率?

100人坐飞机,第一个乘客在座位中随便选一个坐下,第100人正确坐到自己坐位的概率是? - 知乎

TODO 

​​​​​​​12、最大子序和