leetcode994

第一次自己写的时候的代码:

import javafx.util.Pair;

import java.util.*;

public class leetcode994 {
    public int orangesRotting(int[][] grid) {
        int rowLength = grid.length;
        int colLength = grid[0].length;
        Queue<Pair<Integer,Integer>> orangeQueue1 = new LinkedList<>();
        Queue<Pair<Integer,Integer>> orangeQueue2 = new LinkedList<>();
        for (int i = 0;i < rowLength;i++) {
            for (int j = 0;j < colLength;j++) {
                if (grid[i][j] == 2) {
                    orangeQueue1.offer(new Pair<Integer, Integer> (i,j));
                }
            }
        }
        if (orangeQueue1.isEmpty()) {
            for (int p = 0;p < rowLength;p++) {
                for (int q = 0;q < colLength;q++) {
                    if (grid[p][q] == 1) {
                        return -1;
                    }
                }
            }
            return 0;
        }
        int time = 0;
        while (!orangeQueue1.isEmpty() || !orangeQueue2.isEmpty()) {
            if (!orangeQueue1.isEmpty()) {
                process(grid,orangeQueue1,orangeQueue2);
                time++;
            }
            if (!orangeQueue2.isEmpty()) {
                process(grid,orangeQueue2,orangeQueue1);
                time++;
            }
        }
        for (int p = 0;p < rowLength;p++) {
            for (int q = 0;q < colLength;q++) {
                if (grid[p][q] == 1) {
                    return -1;
                }
            }
        }
        return time - 1;
    }

    private void process(int[][] grid,Queue<Pair<Integer,Integer>> orangeQueue1,Queue<Pair<Integer,Integer>> orangeQueue2) {
        while (!orangeQueue1.isEmpty()) {
            Pair<Integer,Integer> orange1 = orangeQueue1.poll();
            changeOrange(grid,orange1.getKey()+1,orange1.getValue(),orangeQueue2);
            changeOrange(grid,orange1.getKey()-1,orange1.getValue(),orangeQueue2);
            changeOrange(grid,orange1.getKey(),orange1.getValue()+1,orangeQueue2);
            changeOrange(grid,orange1.getKey(),orange1.getValue()-1,orangeQueue2);
        }

    }
    private void changeOrange(int[][] grid, int i,int j,Queue<Pair<Integer,Integer>> orangeQueue) {
        if (i > grid.length -1 || i < 0 || j > grid[0].length - 1 || j < 0) {
            return;
        }
        if (grid[i][j] == 1) {
            grid[i][j] = 2;
            orangeQueue.offer(new Pair<Integer, Integer>(i,j));
        }
    }
}
可改进点:
1.用Pair多麻烦啊,看起来太复杂了。改成用长度为2的数组!
2.不要bfs完了之后再去遍历数组看看有没有1剩下!一开始就用一个变量去统计1的数目,然后bfs的时候一个个减掉,看最后有没有剩下!
3.不需要两个队列!!一个就够了!!用一个变量去记录队列的长度!!

改进后的代码:
import javafx.util.Pair;

import java.util.*;

public class leetcode994 {
    public int orangesRotting(int[][] grid) {
        int rowLength = grid.length;
        int colLength = grid[0].length;
        Queue<int[]> orangeQueue = new LinkedList<>();
        int count = 0;
        for (int i = 0;i < rowLength;i++) {
            for (int j = 0;j < colLength;j++) {
                if (grid[i][j] == 1) {
                    count++;
                }
                if (grid[i][j] == 2) {
                    orangeQueue.offer(new int[] {i,j});
                }
            }
        }
        int res = 0;
        while (count > 0 && !orangeQueue.isEmpty()) {
            int size = orangeQueue.size();
            res++;
            for (int p = 0;p < size;p++) {
                int[] orange = orangeQueue.poll();
                int orangeRow = orange[0];
                int orangeCol = orange[1];
                if (orangeRow + 1 < rowLength && grid[orangeRow+1][orangeCol] == 1) {
                    grid[orangeRow+1][orangeCol] = 2;
                    count--;
                    orangeQueue.offer(new int[] {orangeRow+1,orangeCol});
                }
                if (orangeRow -1 >= 0 && grid[orangeRow-1][orangeCol] == 1) {
                    grid[orangeRow-1][orangeCol] = 2;
                    count--;
                    orangeQueue.offer(new int[] {orangeRow-1,orangeCol});
                }
                if (orangeCol + 1 < colLength && grid[orangeRow][orangeCol+1] == 1) {
                    grid[orangeRow][orangeCol+1] = 2;
                    count--;
                    orangeQueue.offer(new int[] {orangeRow,orangeCol+1});
                }
                if (orangeCol - 1 >= 0 && grid[orangeRow][orangeCol-1] == 1) {
                    grid[orangeRow][orangeCol-1] = 2;
                    count--;
                    orangeQueue.offer(new int[] {orangeRow,orangeCol-1});
                }
            }
        }

        if (count != 0) {
            return -1;
        }
        return res;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值