跳转至

带你快速刷完67道剑指offer

这是六则或许对你有些许帮助的信息:

⭐️1、阿秀与朋友合作开发了一个编程资源网站,目前已经收录了很多不错的学习资源和黑科技(附带下载地址),如过你想要寻求合适的编程资源,欢迎体验以及推荐自己认为不错的资源,众人拾柴火焰高,我为人人,人人为我🔥!

2、👉23年5月份阿秀从字节跳动离职跳槽到某外企期间,为方便自己找工作,增加上岸几率,我自己从0开发了一个互联网中大厂面试真题解析网站,包括两个前端和一个后端。能够定向查看某些公司的某些岗位面试真题,比如我想查一下行业为互联网,公司为字节跳动,考察岗位为后端,考察时间为最近一年之类的面试题有哪些?

网站地址:InterviewGuide大厂面试真题解析网站。点此可以查看该网站的视频介绍:B站视频讲解 如果可以的话求个B站三连,感谢!

3、😊 分享一个阿秀自己私藏的黑科技网站点此直达,主要是各类小众实用APP、网站等,除此外也包括高清影视、音乐、电视剧、AI、纪录片、英语四六级考试、考研考公、副业等资源。

4、😍免费分享阿秀个人学习计算机以来收集到的免费学习资源,点此白嫖;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏

5、🚀如果你想在校招中顺利拿到更好的offer,阿秀建议你多看看前人踩过的坑留下的经验,事实上你现在遇到的大多数问题你的学长学姐师兄师姐基本都已经遇到过了。

6、🔥 欢迎准备计算机校招的小伙伴加入我的学习圈子,一个人踽踽独行不如一群人报团取暖,圈子里沉淀了很多过去21/22/23/24/25届学长学姐的经验和总结,好好跟着走下去的,最后基本都可以拿到不错的offer!如果你需要《阿秀的学习笔记》网站中📚︎校招八股文相关知识点的PDF版本的话,可以点此下载

No66、机器人的运动范围

牛客网原题链接

题目描述

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动, 每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。 但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

示例1

输入

Text Only
5,10,10
返回值

Text Only
21

1、借助标记法,看的解释,其实很好理解和明白

C++
bool canReach(int threshold, int x, int y) {
    int sum = 0;
    while (x > 0) {
        sum += x % 10;
        x /= 10;
    }
    while (y > 0) {
        sum += y % 10;
        y /= 10;
    }
    return sum <= threshold;
}

int movingCountCore(int threshold, int i, int rows,int j ,int cols, vector<vector<bool>>&visit) {
    if (i < 0 || i >= rows || j < 0 || j >= cols || !canReach(threshold, i, j) || visit[i][j] == true) return 0;
    //边界值不满足,不能到达或者已经走过了,也到达不了,返回0
    visit[i][j] = true; // 当前已经走过了,并且满足要求,所有后续return 要加上1

    return movingCountCore(threshold, i - 1, rows, j, cols, visit) + //分别是上下左右各个方向判断一下
        movingCountCore(threshold, i + 1, rows, j, cols, visit) +
        movingCountCore(threshold, i , rows, j-1, cols, visit) +
        movingCountCore(threshold, i, rows, j+1, cols, visit) + 1;

}
int movingCount(int threshold, int rows, int cols)
{
    vector<vector<bool>> visit(rows,vector<bool>(cols,false));
    return movingCountCore(threshold, 0,  rows, 0, cols, visit);

}

2、标注借助法的简化版

递归只要俩行就够了,helper(threshold, rows, cols, flags, i + 1, j) + helper(threshold, rows, cols, flags, i, j + 1) + 1,不需要往回走,然后前面的判断i,j也不会小于零了

因为是从(0 0 ),开始走的,所以只需要判断向上和向右的情况即可

C++
bool canReach(int threshold, int x, int y) {
    int sum = 0;
    while (x > 0) {
        sum += x % 10;
        x /= 10;
    }
    while (y > 0) {
        sum += y % 10;
        y /= 10;
    }
    return sum <= threshold;
}

int movingCountCore(int threshold, int i, int rows,int j ,int cols, vector<vector<bool>>&visit) {
    if (i >= rows || j >= cols || !canReach(threshold, i, j) || visit[i][j] == true) return 0;
    //边界值不满足,不能到达或者已经走过了,也到达不了,返回0
    visit[i][j] = true; // 当前已经走过了,并且满足要求,所有后续return 要加上1

    return  movingCountCore(threshold, i + 1, rows, j, cols, visit) +
        movingCountCore(threshold, i, rows, j+1, cols, visit) + 1;

}
int movingCount(int threshold, int rows, int cols)
{
    vector<vector<bool>> visit(rows,vector<bool>(cols,false));
    return movingCountCore(threshold, 0,  rows, 0, cols, visit);

}

3、BFS

C++
bool canReach(int threshold, int x, int y) {
    int sum = 0;
    while (x > 0) {
        sum += x % 10;
        x /= 10;
    }
    while (y > 0) {
        sum += y % 10;
        y /= 10;
    }
    return sum <= threshold;
}

int movingCount(int threshold, int rows, int cols)
{
    vector<vector<bool>> grid(rows,vector<bool>(cols,false));
    queue<pair<int, int>> que;
    if (canReach(threshold, 0, 0)) {
        que.push(make_pair(0, 0));
        grid[0][0] = true;
    }
    int cnt = 0;
    while (!que.empty()) {
        ++cnt;
        int x, y;
        tie(x, y) = que.front();
        que.pop();
        if (x + 1 < rows && !grid[x + 1][y] && canReach(threshold, x + 1, y)) {
            grid[x + 1][y] = true;
            que.push(make_pair(x + 1, y));
        }
        if (y + 1 < cols && !grid[x][y + 1] && canReach(threshold, x, y + 1)) {
            grid[x][y + 1] = true;
            que.push(make_pair(x, y + 1));
        }
    }
    return cnt;

}

二刷:

1、还是比较经典的方法

运行时间:4ms 占用内存:504k

C++
int getValue(int row, int col) {
    int sum = 0;
    while (row != 0)
    {
        sum += row % 10;
        row = row / 10;
    }

    while (col != 0)
    {
        sum += col % 10;
        col = col / 10;
    }
    return sum;
}

void movingCountCore(int threshold, int rows, int cols, vector<vector<bool>>& visit, int row, int col, int &count) {
    if (row < 0 || col < 0 || row >= rows || col >= cols || visit[row][col] == true) return;
    if (getValue(row, col) > threshold) {
        visit[row][col] = true;
        return;
    }
    visit[row][col] = true;
    count++;

    movingCountCore(threshold, rows, cols, visit, row + 1, col, count);
    movingCountCore(threshold, rows, cols, visit, row - 1, col, count);
    movingCountCore(threshold, rows, cols, visit, row, col + 1, count);
    movingCountCore(threshold, rows, cols, visit, row, col - 1, count);

}


int movingCount(int threshold, int rows, int cols)
{
    if (rows < 0 || cols < 0) return 0;
    vector<vector<bool>> visit(rows, vector<bool>(cols, false));
    int count = 0;
    movingCountCore(threshold, rows, cols, visit, 0, 0, count);
    return count;

}