diff --git a/code/lc242.java b/code/lc242.java new file mode 100644 index 0000000000000000000000000000000000000000..e75bbcca4d9e6ae99f62b36557548cdc454490d0 --- /dev/null +++ b/code/lc242.java @@ -0,0 +1,23 @@ +package code; +/* + * 242. Valid Anagram + * 题意:字符串t是否为s打乱后的重排列 + * 难度:Easy + * 分类:Hash Table, Sort + * 思路: + * Tips: + */ +public class lc242 { + public boolean isAnagram(String s, String t) { + if(s.length()!=t.length()) return false; + int[] chs = new int[26]; + for(int i=0; i1){ + d=d/3; + if(d==1) return true; + } + return false; + } +} diff --git a/code/lc329.java b/code/lc329.java new file mode 100644 index 0000000000000000000000000000000000000000..32382e31d73ebd1627edac68efdb28c7ae9d2999 --- /dev/null +++ b/code/lc329.java @@ -0,0 +1,33 @@ +package code; +/* + * 329. Longest Increasing Path in a Matrix + * 题意:寻找最长的递增路径 + * 难度:Hard + * 分类:Depth-first Search, Topological Sort, Memoization + * 思路:带记忆的dfs,之前计算的最优解可以直接合并 + * Tips: + */ +public class lc329 { + public int longestIncreasingPath(int[][] matrix) { + if(matrix.length==0) return 0; + int[][] cache = new int[matrix.length][matrix[0].length]; //存储计算过的结果 + int res = 0; + for (int i = 0; i < matrix.length ; i++) { + for (int j = 0; j < matrix[0].length ; j++) { + res = Math.max(dfs(matrix, i, j, cache), res); + } + } + return res; + } + + public int dfs(int[][] matrix, int i, int j, int[][] cache){ + int max = 1; + if(cache[i][j]!=0) return cache[i][j]; + if( i>0 && matrix[i-1][j]>matrix[i][j]) max = Math.max(dfs(matrix, i-1, j, cache)+1, max); + if( j>0 && matrix[i][j-1]>matrix[i][j]) max = Math.max(dfs(matrix, i, j-1, cache)+1, max); + if( i+1matrix[i][j]) max = Math.max(dfs(matrix, i+1, j, cache)+1, max); + if( j+1matrix[i][j]) max = Math.max(dfs(matrix, i, j+1, cache)+1, max); + cache[i][j] = max; + return max; + } +}