diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T32_70_ClimbStairs.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T32_70_ClimbStairs.java new file mode 100644 index 0000000..daf60eb --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T32_70_ClimbStairs.java @@ -0,0 +1,27 @@ +package com.markilue.leecode.hot100.second; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-26 14:21 + *@Description: TODO 力扣70 爬楼梯: + *@Version: 1.0 + */ +public class T32_70_ClimbStairs { + + public int climbStairs(int n) { + + int[] dp = new int[n + 1]; + + dp[0] = 1; + dp[1] = 1; + + for (int i = 2; i < dp.length; i++) { + dp[i] = dp[i - 1] + dp[i - 2];//走一步 或 走两步 + } + + return dp[n]; + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T33_72_MinDistance.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T33_72_MinDistance.java new file mode 100644 index 0000000..1527113 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T33_72_MinDistance.java @@ -0,0 +1,99 @@ +package com.markilue.leecode.hot100.second; + +import org.junit.Test; + +import java.util.Arrays; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-26 15:10 + *@Description: + * TODO 力扣72 编辑距离: + * 给你两个单词 word1 和 word2, 请返回将 word1 转换成 word2 所使用的最少操作数 。 + * 你可以对一个单词进行如下三种操作: + * 插入一个字符 + * 删除一个字符 + * 替换一个字符 + *@Version: 1.0 + */ +public class T33_72_MinDistance { + + @Test + public void test() { + String word1 = "horse"; + String word2 = "ros"; + System.out.println(minDistance1(word1, word2)); + } + + + /** + * 动态规划法: + * DP[i][j]表示使得word1[0-i]和word2[0-j]相同的最小操作数 + * @param word1 + * @param word2 + * @return + */ + public int minDistance(String word1, String word2) { + + int[][] dp = new int[word1.length() + 1][word2.length() + 1]; + + for (int i = 1; i < dp[0].length; i++) { + dp[0][i] = i; + } + + for (int i = 1; i < dp.length; i++) { + dp[i][0] = i; + } + + for (int i = 1; i < dp.length; i++) { + for (int j = 1; j < dp[0].length; j++) { + if (word1.charAt(i - 1) == word2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } else { + dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;//删除,插入,替换 + } + } + } + + return dp[word1.length()][word2.length()]; + + } + + + int[][] memo; + + /** + * 回溯+记忆化搜索: + * 速度击败100% 内存击败76.8% 2ms + * @param word1 + * @param word2 + * @return + */ + public int minDistance1(String word1, String word2) { + memo = new int[word1.length()][word2.length()]; + for (int i = 0; i < memo.length; i++) { + Arrays.fill(memo[i], -1); + } + return dp(word1, word2, word1.length()-1, word2.length()-1); + } + + private int dp(String word1, String word2, int i, int j) { + if (i == -1) { + return j+1; + } + if (j == -1) { + return i+1; + } + if (memo[i][j] != -1) { + return memo[i][j]; + } + if (word1.charAt(i) == word2.charAt(j)) { + memo[i][j] = dp(word1, word2, i - 1, j - 1); + } else { + memo[i][j] = Math.min(Math.min(dp(word1, word2, i - 1, j - 1), dp(word1, word2, i - 1, j)), dp(word1, word2, i, j - 1))+1; + } + return memo[i][j]; + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java new file mode 100644 index 0000000..04a8fb2 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java @@ -0,0 +1,53 @@ +package com.markilue.leecode.hot100.second; + +import org.junit.Test; + +import java.util.Arrays; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-27 10:25 + *@Description: + * TODO 力扣75 颜色分类: + * 给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums ,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。 + * 我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。 + * 必须在不使用库内置的 sort 函数的情况下解决这个问题。 + *@Version: 1.0 + */ +public class T34_75_SortColors { + + @Test + public void test() { + int[] nums = {2,0,2,1,1,0}; + sortColors(nums); + System.out.println(Arrays.toString(nums)); + } + + //遇上0就交换,遇上1就交换1 + public void sortColors(int[] nums) { + + int index0 = 0; + int index1 = 0; + + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 0) { + swap(nums, i, index0); + if (index0 < index1) swap(nums, i, index1);//换过去的是1,触发交换 ->好像不是原地排序了,交换之后前面的1跑到后面了 + index0++; + index1++; + } else if (nums[i] == 1) { + swap(nums, i, index1); + index1++; + } + } + + } + + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T35_76_MinWindow.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T35_76_MinWindow.java index b533d7f..cb4016c 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T35_76_MinWindow.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T35_76_MinWindow.java @@ -19,7 +19,13 @@ public class T35_76_MinWindow { @Test public void test() { String s = "aa", t = "aa"; - System.out.println(minWindow(s, t)); + System.out.println(minWindow1(s, t)); + } + + @Test + public void test1() { + String s = "ADOBECODEBANC", t = "ABC"; + System.out.println(minWindow1(s, t)); } @@ -63,7 +69,7 @@ public class T35_76_MinWindow { char c1 = s.charAt(left++); if (need.containsKey(c1)) { Integer num = window.get(c1); - if (num .equals(need.get(c1)) ) { + if (num.equals(need.get(c1))) { count--; } @@ -77,4 +83,52 @@ public class T35_76_MinWindow { return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len - 1); } + + + public String minWindow1(String s, String t) { + + HashMap need = new HashMap<>(); + + for (char c : t.toCharArray()) { + need.put(c, need.getOrDefault(c, 0) + 1); + } + + HashMap window = new HashMap<>(); + + int left = 0; + int right = 0; + int count = 0; + int result = Integer.MAX_VALUE; + int start = 0; + while (right < s.length()) { + char c = s.charAt(right++); + + if (need.containsKey(c)) { + window.put(c, window.getOrDefault(c, 0) + 1); + if (window.get(c).equals(need.get(c))) { + count++; + } + } + + while (left < right && count == need.size()) { + int length = right - left ; + if (length < result) { + result = length; + start = left; + } + char c1 = s.charAt(left++); + if (need.containsKey(c1)) { + Integer num = window.get(c1); + if (num .equals(need.get(c1)) ) { + count--; + } + window.put(c1, num - 1); + + } + } + } + + return result == Integer.MAX_VALUE ? "" : s.substring(start, start + result); + + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T36_78_Subsets.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T36_78_Subsets.java new file mode 100644 index 0000000..4ecb56e --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T36_78_Subsets.java @@ -0,0 +1,36 @@ +package com.markilue.leecode.hot100.second; + +import java.util.ArrayList; +import java.util.List; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-04-27 11:55 + *@Description: + * TODO 力扣78 子集: + * 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。 + * 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 + *@Version: 1.0 + */ +public class T36_78_Subsets { + + List cur = new ArrayList<>(); + List> result = new ArrayList<>(); + + public List> subsets(int[] nums) { + backtracking(nums,0); + return result; + } + + public void backtracking(int[] nums, int start) { + result.add(new ArrayList<>(cur)); + + for (int i = start; i < nums.length; i++) { + cur.add(nums[i]); + backtracking(nums,i+1); + cur.remove(cur.size()-1); + } + } +}