第一次自己写的时候的代码:
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;
}
}