1、Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
Example 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:
[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.
Note: The length of each dimension in the given grid does not exceed 50.
递归:
栈:
解题思路:首先就是遍历二维数组的全部元素,为1的为连起来的岛屿,为0的为水面,找到最大的岛屿的面积,遍历的时候只要找到为1的土地,便通过递归或循环栈的方式上下左右寻找将这片岛屿的面积求出来并与上一个进行比较,并在循环求这片岛屿的面积时,将这片岛屿全部置为0,因为在后面的遍历中没用了已经,最后面积即是最大的岛屿面积。
Python代码(两个):
(递归)
class Solution:
def dfs(self, grid, cur_i, cur_j):#将这个二维数组索引
if cur_i < 0 or cur_j < 0 or cur_i == len(grid) or cur_j == len(grid[0]) or grid[cur_i][cur_j] != 1:
return 0
grid[cur_i][cur_j] = 0#将这个土地清零,保存面积
ans = 1
for di, dj in [[0, 1], [0, -1], [1, 0], [-1, 0]]:#上下左右搜索岛屿,这里上下左右会避免延伸出去的岛屿被漏掉
next_i, next_j = cur_i + di, cur_j + dj
ans += self.dfs(grid, next_i, next_j)#这里递归如若碰到为零的情况则直接返回0
return ans#最后返回这片岛屿的面积值
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
ans = 0
for i, l in enumerate(grid):#将每一行的索引及每个列表迭代
for j, n in enumerate(l):#将这一行里每个元素及索引迭代
ans = max(self.dfs(grid, i, j), ans)#将每一次得到的面积取最大值,每个岛屿计算完会直接置0
return ans
(栈)
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
ans = 0
for i, l in enumerate(grid):
for j, n in enumerate(l):
cur = 0
stack = [(i, j)]
while stack:
cur_i, cur_j = stack.pop()#删除栈中最后一个元素并扔出
if cur_i < 0 or cur_j < 0 or cur_i == len(grid) or cur_j == len(grid[0]) or grid[cur_i][cur_j] != 1:
continue#不符合要求或这片不是土地的直接跳出while循环
cur += 1#当前这边岛屿的面积值
grid[cur_i][cur_j] = 0#将本片岛屿置零
for di, dj in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
next_i, next_j = cur_i + di, cur_j + dj#向上下左右延伸
stack.append((next_i, next_j))#可以理解为栈中元素始终只有一个,只要栈中为空则输出面积取最大值,并进行下一个索引
ans = max(ans, cur)
return ans
2、
Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.
A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
Example 1:
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column
Example 2:
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example 3:
Input: matrix = [[7,8],[1,2]]
Output: [7]
Constraints:
m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 10^5.
All elements in the matrix are distinct.
解题思路:思路简单,暴力双重循环遍历矩阵元素,先找出此行最小的元素,再下去循环此列有没有比这个元素更大的元素。
C代码:
/**
- Note: The returned array must be malloced, assume caller calls free().
*/
int getmin(int ar[], int len) {
int min = ar[0], i, temp = 0;
for (i = 1; i < len; i++) {
if (min > ar[i]) {
min = ar[i];
temp = i;
}
}
return temp;
}
int* luckyNumbers(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
int m = matrixSize, n = *matrixColSize, i, j, count = 0, mini = 0, temp = 0;
int ret = (int)malloc(sizeof(int)*m);//动态分配内存,假设调用者会调用free()
if (m == 0) {
*returnSize = 0;
return ret;
}
for (i = 0; i < m; i++) {
mini = getmin(matrix[i], n);
temp = matrix[i][mini];
for (j = 0; j < m; j++) {
if (j == i) {
continue;
}
if (matrix[j][mini] > matrix[i][mini]) {
temp = matrix;
}
}
if (matrix[i][mini] == temp) {
ret[count++] = temp;
}
}
*returnSize = count;
return ret;
}
Python代码:
class Solution:
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)#行
n = len(matrix[0])#列
if m == 0:
return []
fortunate = []
for i in range(m) :#行遍历
maxi = min(matrix[i])
maxindex = matrix[i].index(maxi)
for j in range(m) :#对这一列遍历
if j == i:
continue
if matrix[j][maxindex] > maxi:
maxi = matrix[j][maxindex]
if maxi == min(matrix[i]) :
fortunate.append(maxi)
return fortunate