leecode,rt_phm更新更新
This commit is contained in:
parent
0c74438f55
commit
4d08683952
|
|
@ -137,4 +137,33 @@ public class LC_1254_ClosedIsland {
|
||||||
return dfs(grid, i - 1, j) & dfs(grid, i + 1, j) & dfs(grid, i, j - 1) & dfs(grid, i, j + 1);
|
return dfs(grid, i - 1, j) & dfs(grid, i + 1, j) & dfs(grid, i, j - 1) & dfs(grid, i, j + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//如何为封闭岛屿:有边界
|
||||||
|
public int closedIsland3(int[][] grid) {
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < grid.length; i++) {
|
||||||
|
for (int j = 0; j < grid[0].length; j++) {
|
||||||
|
if (grid[i][j] == 0) {
|
||||||
|
if (find(grid, i, j)) {
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean find(int[][] grid, int i, int j) {
|
||||||
|
if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length) {
|
||||||
|
return false;//碰到了边界还没有返回,则不是封闭岛屿
|
||||||
|
}
|
||||||
|
if (grid[i][j] == 1 || grid[i][j] == 2) {
|
||||||
|
//遇到了边界
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
grid[i][j] = 2;
|
||||||
|
return find(grid, i + 1, j) & find(grid, i - 1, j) & find(grid, i, j + 1) & find(grid, i, j - 1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,4 +99,22 @@ public class T49_124_MaxPathSum {
|
||||||
return Math.max(left, right) + root.val;
|
return Math.max(left, right) + root.val;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int maxPathSum3(TreeNode root) {
|
||||||
|
findCurMax(root);
|
||||||
|
return maxSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
//返回要当前节点的最大值;不要的情况已经在子节点中讨论过了
|
||||||
|
public int findCurMax(TreeNode node) {
|
||||||
|
if (node == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int left = Math.max(findCurMax(node.left), 0);
|
||||||
|
int right = Math.max(findCurMax(node.right), 0);
|
||||||
|
|
||||||
|
maxSum = Math.max(maxSum, left + right + node.val);
|
||||||
|
return Math.max(left, right) + node.val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ public class T67_221_MaximalSquare {
|
||||||
{'1', '1', '1', '1', '1'},
|
{'1', '1', '1', '1', '1'},
|
||||||
{'1', '0', '0', '1', '0'}
|
{'1', '0', '0', '1', '0'}
|
||||||
};
|
};
|
||||||
System.out.println(maximalSquare1(matrix));
|
System.out.println(maximalSquare2(matrix));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -100,4 +100,34 @@ public class T67_221_MaximalSquare {
|
||||||
return result * result;
|
return result * result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int maximalSquare2(char[][] matrix) {
|
||||||
|
int m = matrix.length;
|
||||||
|
int n = matrix[0].length;
|
||||||
|
int[][] dp = new int[m][n];
|
||||||
|
int result = Integer.MIN_VALUE;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
dp[0][i] = matrix[0][i] == '1' ? 1 : 0;
|
||||||
|
result = Math.max(result,dp[0][i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
dp[i][0] = matrix[i][0] == '1' ? 1 : 0;
|
||||||
|
result = Math.max(result,dp[0][i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < m; i++) {
|
||||||
|
for (int j = 1; j < n; j++) {
|
||||||
|
if (matrix[i][j] == '1') {
|
||||||
|
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
|
||||||
|
}
|
||||||
|
if (result < dp[i][j]) result = dp[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result * result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,4 +33,22 @@ public class T69_234_IsPalindrome {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPalindrome1(ListNode head) {
|
||||||
|
root = head;
|
||||||
|
return find(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean find(ListNode node) {
|
||||||
|
if (node == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (find(node.next) && node.val == root.val) {
|
||||||
|
root = root.next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.markilue.leecode.interview.meituan.T0415;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.interview.meituan.T0415
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-06-06 11:38
|
||||||
|
*@Description:
|
||||||
|
* TODO 字符串前缀:
|
||||||
|
* 现在有两个字符串S和T,你需要对S进行若干次操作,使得S是T的一个前缀(空串也是一个前缀)。
|
||||||
|
* 每次操作可以修改S的一个字符,或者删除一个S末尾的字符。小团需要写一段程序,输出最少需要操作的次数。
|
||||||
|
*
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question1 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int count = sc.nextInt();
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
String S = sc.next();
|
||||||
|
String T = sc.next();
|
||||||
|
solve(S, T);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//为什么不是动态规划?因为题目要求,删只能删除S的末尾
|
||||||
|
private static void solve(String s, String t) {
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
int pos = s.length() - 1;
|
||||||
|
//如果S是T的前缀,则S一定要比T短
|
||||||
|
if (s.length() > t.length()) {
|
||||||
|
result += s.length() - t.length();
|
||||||
|
pos = t.length() - 1;
|
||||||
|
}
|
||||||
|
//能修改就修改;不能修改再删除
|
||||||
|
for (int i = pos; i >= 0; i--) {
|
||||||
|
if (t.charAt(i) != s.charAt(i)) {
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue