岛屿最大面积_组合总和
【摘要】 岛屿的最大面积岛屿的最大面积class Solution { int[][] next = {{1,0},{-1,0},{0,-1},{0,1}}; public int DSF(int[][] grid,int row,int col,int sr,int sc){ //标记 int count = 1; grid[sr][sc] =...
岛屿的最大面积
class Solution {
int[][] next = {{1,0},{-1,0},{0,-1},{0,1}};
public int DSF(int[][] grid,int row,int col,int sr,int sc){
//标记
int count = 1;
grid[sr][sc] = -1;
for(int i = 0;i<next.length;i++){
int x = sr + next[i][0];
int y = sc + next[i][1];
if(x<0||x>=row||y<0||y>=col){
//越界
continue;
}
if(grid[x][y]==1){
//注意重点!!! count开始时要赋值为 1
//因为进了这个函数就说明是陆地!
count += DSF(grid,row,col,x,y);
}
}
return count;
}
public int maxAreaOfIsland(int[][] grid) {
int maxArea = 0;
int row = grid.length;
int col = grid[0].length;
for(int i = 0;i<row;i++){
for(int j = 0;j<col;j++){
if(grid[i][j]==1){
maxArea = Math.max(maxArea,DSF(grid,row,col,i,j));
}
}
}
return maxArea;
}
}
电话号码的字母组合
class Solution {
public static void DFS(HashMap <Character,String> map, List<String> result, char[] d, StringBuilder str,int pos){
if(pos==d.length){//递归出口!
result.add(str.toString());
return;
}
//拿到 value字符串!
//这里不能套循环遍历,因为这里不能重复!
String tmp = map.get(d[pos]);
for(int j = 0;j<tmp.length();j++){
//遍历该字符串!
//添加该字符!
str.append(tmp.charAt(j));
//进入下一层 深度!
DFS(map, result, d, str, pos+1);
//回退!
//str.delete(str.length()-1,str.length());
str.deleteCharAt(str.length()-1);
}
}
public static List<String> letterCombinations(String digits) {
List<String> result = new LinkedList<>();
if(digits.length()==0){
return result;
}
char[] d = digits.toCharArray();
String[] table = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
StringBuilder str = new StringBuilder();
HashMap<Character,String> map = new HashMap<>();
for(int i =0;i<d.length;i++){
map.put(d[i],table[d[i]-'0']);
}
DFS(map,result,d,str,0);
return result;
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)