領扣LintCode演算法問題答案-1906. 尋找比周圍都大的點
目錄
- 1906. 尋找比周圍都大的點
- 描述
- 樣例 1:
- 題解
- 鳴謝
1906. 尋找比周圍都大的點
描述
給一個nm大小的矩陣,尋找矩陣中所有比鄰居(上下左右,對角也算,不考慮邊界就是8個咯)都嚴格大的點,
回傳一個nm大小的矩陣,如果原矩陣中的點比鄰居都嚴格大,則該位置為1,反之為0,
- 1 ≤ n,m ≤ 100
樣例 1:
輸入:
1 2 3
4 5 8
9 7 0
輸出:
0 0 0
0 0 1
1 0 0
題解
public class Solution {
/**
* @param grid: a matrix
* @return: Find all points that are strictly larger than their neighbors
*/
public int[][] highpoints(int[][] grid) {
// write your code here
final int[][] ps = new int[][]{new int[]{-1,-1}, new int[]{-1, 0}, new int[]{0, -1}, new int[]{0, 1}, new int[]{1,-1}, new int[]{1,0}, new int[]{1,1}};
if (grid == null) {
return grid;
}
int r = grid.length;
if (r == 0) {
return grid;
}
int c = grid[0].length;
if (c == 0) {
return grid;
}
int[][] ret = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
boolean max = true;
for (int[] p : ps) {
int row = p[0] + i;
int col = p[1] + j;
if (row >= 0
&& row < r
&& col >=0
&& col < c) {
if (grid[row][col] >= grid[i][j]) {
max = false;
break;
}
}
}
if (max) {
ret[i][j] = 1;
} else {
ret[i][j] = 0;
}
}
}
return ret;
}
}
原題鏈接點這里
鳴謝
非常感謝你愿意花時間閱讀本文章,本人水平有限,如果有什么說的不對的地方,請指正,
歡迎各位留言討論,希望小伙伴們都能每天進步一點點,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/169992.html
標籤:其他
