刷题
Arrays.sort(num ,comparator) 比较器重写compara方法
int compare(T o1, T o2) 是 “比较 o1 和 o2 的大小”。返回 “负数”,意味着 “o1 比 o2 小”;返回 “零”,意味着 “o1 等于 o2”;返回 “正数”,意味着 “o1 大于 o2”
Integer[] pArrays ; Arrays.sort( pArrays, (a,b)->{return a-b;}); 从小到大 ,(a,b)->{ b-a}从大到小.
切割子串 String.split() set.contains()
List转换为 S
Leetcode 1996
Arrays.sort(properties,(p1,p2)->{
if(p2[1]!=p1[1]){
return p2[1]-p1[1];
}else {
return p1[0]-p2[0];
}
});
int sub = 0 ;
int count =0;
int subx=0;
for(int i = 0;i<properties.length;i++){
if(properties[i][0]>=sub ){
sub= properties[i][0];
subx=properties[i][1];
}else{
count++;
}
}
return count;
代码条件的比较 > >=
Leetcode1765
开始没什么思路,想到用BFS,但没做出来,后面查看资料需要运用到多源BFS,和BFS差别不大
int len = isWater[0].length;
int [][] res = new int[len][len];
Queue<int []> queue= new LinkedList<>();
for(int i = 0 ;i<len;i++){
for(int j =0;j<len;j++){
if(isWater[i][j]==0){
queue.add(new int []{i,j});
res[i][j]=0;
}else{
res[i][j]=-1;
}
}
}
int [][] dires= {{1,0},{-1,0},{0,1},{0,-1}};
int heigh=0;
while(!queue.isEmpty()){
int [] ans = queue.poll();
for(int [] dir : dires){
int nx = ans[0]+dir[0];
int ny = ans[1] +dir[1];
if(nx<0||nx>=len || ny<0|| ny>=len ){
continue;
}
if(res[nx][ny]!=-1){
continue;
}
res[nx][ny]= res[ans[0]][ans[1]]+1;
queue.add(new int []{nx,ny});
}
}
return res;
- 样例错误

边界条件
1 才是水域 不是0是水域
还有可以通过动态规划的方法 这里没有思考
Leetcode 994
int m = grid.length, n= grid[0].length;
int[][] board = new int[m][n];
int count=0;
int res=0;
Queue<int[]> queue= new LinkedList<>();
for(int i = 0; i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==0){
}else if(grid[i][j]==1){
count++;
}else{
board[i][j]=0;
queue.add(new int[]{i,j});
}
}
}
if(count ==0){
return 0;
}
int max=0;
int[][] dirs = {{1,0},{-1,0},{0,-1},{0,1}};
while(!queue.isEmpty()){
int[] ans = queue.poll();
for(int [] dir : dirs){
int x= ans[0]+dir[0];
int y = ans[1]+dir[1];
if(x<0|| x>=m|| y<0|| y>=n ){
continue;
}
if(grid[x][y]==0 || grid[x][y]==2){
continue;
}
board[x][y] = board[ans[0]][ans[1]] +1;
max = Math.max(board[x][y],max);
grid[x][y]=2;
queue.add(new int[]{x,y});
count--;
}
}
return count==0? max:-1;
边界思考
暂无
leetcode 884
写博客是为了让别人能明白,之后在是自己明白. 如果有不明白的地方欢迎加Q3378404370 讨论

浙公网安备 33010602011771号