Leetcode:3195
1,题目
2,思路
首先找到上下左右初始边就开始循环找到上下左右最终边,做面积运算就好了(其中+1是因为下标比实际位置少1)
3,代码
class Solution3195 {
public int minimumArea(int[][] grid) {
int above = 0;//上
int under = 0;//下
int left = 0;//左
int right = 0;//右
boolean b = true;
for (int i = 0; i < grid.length; i++) {
if (b) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
above = i;
under = i;
left = j;
right = j;
b = false;
break;
}
}
} else break;
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
if (left >= j) {//左
left = j;
}
if (right <= j) {//右
right = j;
}
if (above >= i) {//上
above = i;
}
if (under <= i) {//下
under = i;
}
}
}
}
return (right - left + 1) * (under - above + 1);
}
}