• 面试必考精华版Leetcode994. 腐烂的橘子


    题目:


    代码(2023年10月26日 首刷自解):

    1. class Solution {
    2. public:
    3. int orangesRotting(vectorint>>& grid) {
    4. /*统计grid二维数组中有多少新鲜橘子和多少烂橘子,将所有的烂橘子存入一个三元队列中
    5. 其中,队列前两个参数表示烂橘子坐标,第三个参数代表是第几代烂橘子
    6. 同时,统计新鲜橘子数量*/
    7. int m = grid.size();
    8. int n = grid[0].size();
    9. int fresh=0;
    10. queueint,int,int>> q;
    11. for(int i=0;i
    12. for(int j=0;j
    13. if(grid[i][j]==1){
    14. fresh++;//记录新鲜橘子数量
    15. }else if(grid[i][j]==2){
    16. q.emplace(i,j,0);//将烂橘子存入队列,第三个参数0表示都是最初的烂橘子
    17. }
    18. }
    19. }
    20. vector<int> dx ={1,0,-1,0};
    21. vector<int> dy ={0,1,0,-1};
    22. int time =0;//记录队列最后一个烂橘子是第几代
    23. while(!q.empty()){
    24. auto [cx,cy,d]=q.front();
    25. time=d;
    26. q.pop();
    27. for(int i=0;i<4;++i){
    28. int nx=cx+dx[i];
    29. int ny=cy+dy[i];
    30. //检查烂橘子四个方向上的是否在二维数组内部且是否是新鲜橘子
    31. if(nx>=0 && nx=0 && ny1){
    32. grid[nx][ny]=2;//将其变成烂橘子
    33. fresh--;//新鲜橘子减少
    34. q.emplace(nx,ny,d+1);//将烂橘子存入队列,并且标记其是第几代烂橘子
    35. }
    36. }
    37. }
    38. if(fresh>0){
    39. return -1;
    40. }else{
    41. return time;
    42. }
    43. }
    44. };

    代码(二刷瞄了眼思路 2024年3月7日)

    1. class Solution {
    2. public:
    3. void BFS(vectorint>>& grid, int row, int column, queueint,int>>& q) {
    4. int nr = grid.size();
    5. int nc = grid[0].size();
    6. if (row - 1 >= 0 && grid[row - 1][column] == 1)
    7. { grid[row-1][column] = 2; q.push(make_pair(row - 1, column));}
    8. if (row + 1 < nr && grid[row + 1][column] == 1)
    9. { grid[row+1][column] = 2; q.push(make_pair(row + 1, column));}
    10. if (column - 1 >= 0 && grid[row][column - 1] == 1)
    11. { grid[row][column - 1] = 2; q.push(make_pair(row, column - 1));}
    12. if (column + 1 < nc && grid[row][column + 1] == 1)
    13. { grid[row][column + 1] = 2; q.push(make_pair(row, column + 1));}
    14. }
    15. int orangesRotting(vectorint>>& grid) {
    16. int nr = grid.size();
    17. if (nr == 0) return 0;
    18. int nc = grid[0].size();
    19. queueint,int>> q;
    20. int orange = 0;
    21. for (int i = 0; i < nr; ++i) {
    22. for (int j = 0; j < nc; ++j) {
    23. if (grid[i][j] == 1 || grid[i][j] == 2) orange++;
    24. if (grid[i][j] == 2) {
    25. q.push(make_pair(i, j));
    26. }
    27. }
    28. }
    29. int time = 0;
    30. int toxic_orange = 0;
    31. while (!q.empty()) {
    32. int size = q.size();
    33. while (size--) {
    34. auto it = q.front();
    35. toxic_orange++;
    36. q.pop();
    37. BFS(grid, it.first, it.second, q);
    38. }
    39. time++;
    40. }
    41. if (time) time--;
    42. return orange == toxic_orange ? time : -1;
    43. }
    44. };

  • 相关阅读:
    Nginx网站服务(nginx监控模块vts、nginx配置文件介绍、nginx多种访问配置)
    17-ROS tf包介绍
    学单片机怎么在3-5个月内找到工作?
    Sanitizers 系列之 address sanitizer 原理篇
    R语言绘图—制作你的专属“足迹“图
    【校招VIP】前端校招考点之UDP
    Cortex-A9 架构
    Ubuntu18.04安装onnxruntime(c++版)与CUDA运行Demo
    摩根大通研究论文:大型语言模型+自动规划器用来作有保障的旅行规划
    一文读懂spring.factories作用
  • 原文地址:https://blog.csdn.net/qq_52313711/article/details/134051151