From 385e14423ed7c716c8ec257aca39cac4e557f539 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 24 Mar 2024 18:48:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=8E=E5=9B=A2=E9=A2=98=E7=9B=AE=20LeetCode?= =?UTF-8?q?=E5=A4=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interview/Meituan/MeiTuan24_3_9.java | 328 ++++++++++++++++++ .../whaifree/leetCode/Array/LeetCode713.java | 64 ++++ .../cn/whaifree/redo/redo_24_3_24/LCR001.java | 135 +++++++ .../cn/whaifree/redo/redo_24_3_24/LCR090.java | 54 +++ .../redo/redo_24_3_24/LeetCode005.java | 92 +++++ .../redo/redo_24_3_24/LeetCode1049.java | 79 +++++ .../redo/redo_24_3_24/LeetCode343.java | 52 +++ .../redo/redo_24_3_24/LeetCode416.java | 100 ++++++ .../redo/redo_24_3_24/LeetCode45.java | 48 +++ .../redo/redo_24_3_24/LeetCode647.java | 84 +++++ .../redo/redo_24_3_24/LeetCode713.java | 45 +++ .../redo/redo_24_3_24/LeetCode738.java | 44 +++ .../redo/redo_24_3_24/LeetCode96.java | 62 ++++ src/main/java/cn/whaifree/test/MT.java | 138 ++++++++ 14 files changed, 1325 insertions(+) create mode 100644 src/main/java/cn/whaifree/interview/Meituan/MeiTuan24_3_9.java create mode 100644 src/main/java/cn/whaifree/leetCode/Array/LeetCode713.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LCR001.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LCR090.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode005.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode1049.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode343.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode416.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode45.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode647.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode713.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode738.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode96.java create mode 100644 src/main/java/cn/whaifree/test/MT.java diff --git a/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24_3_9.java b/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24_3_9.java new file mode 100644 index 0000000..7a403b4 --- /dev/null +++ b/src/main/java/cn/whaifree/interview/Meituan/MeiTuan24_3_9.java @@ -0,0 +1,328 @@ +package cn.whaifree.interview.Meituan; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/23 14:28 + * @注释 + */ +public class MeiTuan24_3_9 { + + /** + * MT 是美团的缩写,因此小美很喜欢这两个字母。 + * 现在小美拿到了一个仅由大写字母组成字符串,她可以最多操作k次,每次可以修改任意一个字符。 + * 小美想知道,操作结束后最多共有多少个'M'和''字符? + * + * 输入描述 + * + * 第一行输入两个正整数n,k,代表字符串长度和操作次数。第二行输入一个长度为n的、仅由大写字母组成的字符串。1<=k<=n<=10^5输出描述 + * + * 输出操作结束后最多共有多少个'M'和'T'字符 + */ + static class Problem_1{ + + public static void main(String[] args) { + + /** + * next()方法读取到 空白符(空格,回车,tab) 就结束l; + * nextLine()读取到 回车 结束也就是“\r”; + */ + Scanner scanner = new Scanner(System.in); + int n, k; + n = scanner.nextInt(); //读取int 自动跳过空格、换行、制表符 + k = scanner.nextInt(); // 读取int + String s = scanner.next(); // 读取空格、制表符和换行符 + // nextLine读取 回车 ;nextLine()自动读取了被next()去掉的Enter作为它的结束符 + // 解决方法:在每一个 next()、nextDouble() 、 nextFloat()、nextInt() 等语句之后加一个nextLine()语句,将被next()去掉的Enter等结束符过滤掉 + int isMT = 0; + for (int i = 0; i < n; i++) { + if (s.charAt(i) == 'M' || s.charAt(i) == 'T') { + isMT++; + } + } + + System.out.println(Math.min(isMT + k, s.length())); + } + + + + } + + static class Problem_2{ + /** + * 小美拿到了一个由正整数组成的数组,但其中有一些元素是未知的(用0来表示)。 + * 现在小美想知道,如果那些未知的元素在区间[l,r]范围内随机取值的话,数组所有元素之和的最小值和最大值分别是多少? + * 共有q次询问。 + * 输入描述 + * 第一行输入两个正整数n,q,代表数组大小和询问次数。 + * 第二行输入n个整数ai,其中如果输入ai的为 0,那么说明ai是未知的。 + * 接下来的q行,每行输入两个正整数r,代表一次询问。 + * + * @param args + */ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int q = scanner.nextInt(); + int isUnKnow = 0; + long sum = 0; + for (int i = 0; i < n; i++) { + int j = scanner.nextInt(); + if (j == 0) { + isUnKnow++;// 未知个数 + } + sum += j; + } + + + for (int i = 0; i < q; i++) { + long left = scanner.nextInt(); + long right = scanner.nextInt(); + System.out.println(sum + left * isUnKnow + " " +(sum + right * isUnKnow)); + } + } + } + + static class Problem_3 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int[][] ints = new int[n][n]; + for (int i = 0; i < n; i++) { + String next = scanner.next(); + for (int j = 0; j < n; j++) { + ints[i][j] = next.charAt(j) - 48; + } + } + for (int i = 1; i <= n; i++) { + int cnt = 0; + for (int x = 1; x <= n - i + 1; x++) { + for (int y = 1; y <= n - i + 1; y++) { + int sum = ints[x + i - 1][y + i - 1] - ints[x + i - 1][y - 1] - ints[x - 1][y + i - 1] + ints[x - 1][y - 1]; + if (sum * 2 == i * i) { + cnt++; + } + } + } + System.out.println(cnt); + } + } + + } + + + static class Problem_4 { + + /** + * 区间删除 + * 小美拿到了一个大小为n的数组, + * 她希望删除一个区间后, + * 使得剩余所有元素的乘积末尾至少有k个 0。 + * 小美想知道,一共有多少种不同的删除方案? + * @param args + */ + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int k = scanner.nextInt(); + int[] ints = new int[n]; + for (int i = 0; i < n; i++) { + int j = scanner.nextInt(); + ints[i] = j; + } + + staticYZ(ints, k); + + } + + public static void staticYZ(int[] ints,int k) { + + /** + * 起始只要统计2和5的数量,用双指针,只要2和5的因子数量满足(2^p*5^p)>10^k p=min(i,j) + * 即p>k就满足条件,并且此时指针变大一定会更大 + */ + // 统计全部的因子数量,和每个元素的2和5的因子数量 + int[] two = new int[ints.length]; + int[] five = new int[ints.length]; // 表示每个元素对应因子为5的数量 + int twoESum = 0; + int fiveESum = 0; + for (int i = 0; i < ints.length; i++) { + int twoE = calcYZSL(ints[i], 2); + int fiveE = calcYZSL(ints[i], 5); + twoESum += twoE; + fiveESum += fiveE; + two[i] += twoE; + five[i] += fiveE; + } + +// for (int i = 0; i < ints.length; i++) { +// int anInt = ints[i]; +// while (anInt % 2 == 0) { +// anInt /= 2; +// twoESum++; +// two[i]++; +// } +// while (anInt % 5 == 0) { +// anInt /= 5; +// fiveESum++; +// five[i]++; +// } +// } + + long res = 0; + int left = 0; + int right = 0; + while (right < ints.length) { + twoESum -= two[right]; // twoESum表示不在滑动窗口区间的2总因子数 + fiveESum -= five[right]; + while (Math.min(twoESum, fiveESum) < k && left <= right) { + // 如果右边存在的因子总数太小,则左边因子很大, + // 可以让Left指针右移动,直到满足小的范围,则此时的r-l+1就是一次res + twoESum += two[left]; + fiveESum += five[left]; + left++; + } + + res +=(long) (right - left + 1); + right++; + } + System.out.println(res); + } + + /** + * 计算num,因子k的数量 + * @param num + * @param k + * @return -1表示不是因子,否则返回其因子 10,5返回2 + */ + public static int calcYZSL(int num, int k) { + int res = 0; + while (num % k == 0) { + num /= k; + res++; + } + return res; + } + + + public void delete(int[] nums, int k) { + int left = 0; + int right = 0; + int pro = 1; + int res = 0; + while (right < nums.length) { + pro *= nums[right]; + while (isHasKZero(pro, k)) { + res++; + pro /= nums[left]; + left++; + } + right++; + } + System.out.println(res); + } + +// public static void backTracking(int[] ints, int start, int sum, int k) { +//// if (isTrue(sum, k)) { +//// res++; +//// }else { +//// return; +//// } +// if (start > ints.length) { +// return; +// } +// +// for (int i = start; i < ints.length; i++) { +// backTracking(ints, i + 1, sum / ints[i], k); +// } +// } + + public static boolean isHasKZero(int sum, int k) { + for (int i = 0; i < k; i++) { + if (sum % 10 == 0) { + sum /= 10; + }else { + return false; + } + } + return true; + } + } + + /** + * 关键在于数组能不能通过i直达j + */ + static class Problem_5{ + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + int numberPerson = scanner.nextInt(); + int relation = scanner.nextInt(); + int operation = scanner.nextInt(); + boolean[][] isKnow = new boolean[numberPerson][numberPerson]; + for (int i = 0; i < relation; i++) { + int j = scanner.nextInt()-1; + int e = scanner.nextInt()-1; + if (j > e) { + isKnow[e][j] = true; + }else { + isKnow[j][e] = true; + } + } + + for (int i = 0; i < operation; i++) { + int opr = scanner.nextInt();//操作类型 + int j = scanner.nextInt()-1; + int e = scanner.nextInt()-1; + if (opr == 1) { + if (j>e){ + isKnow[e][j] = false; + }else { + isKnow[j][e] = false; + } + }else{ + if (judgeIsKnow(isKnow, j, e, 0) || judgeIsKnow(isKnow, e, j, 0)) { + System.out.println("yes"); + } else { + System.out.println("No"); + } + } + } + } + + + /** + * i能不能到达j + * @param know + * @param i 出发节点 + * @param j 结束节点 + * @param k 记录次数,防止 + * @return + */ + public static boolean judgeIsKnow(boolean[][] know, int i, int j, int k) { + if (k == know.length) { + return false; + } + if (know[i][j]) { + return true; + } + for (int l = 0; l < know.length; l++) { + if (know[i][l]){ + if (judgeIsKnow(know,l, j, l + 1)||judgeIsKnow(know,j, l, l + 1)) { + return true; + } + } + } + + + return false; + } + + + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode713.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode713.java new file mode 100644 index 0000000..6bee3ed --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode713.java @@ -0,0 +1,64 @@ +package cn.whaifree.leetCode.Array; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/23 16:13 + * @注释 + */ +public class LeetCode713 { + + @Test + public void test() { + int[] nums = {1,2,3}; + int k = 0; + int res = new Solution1().numSubarrayProductLessThanK(nums, k); + System.out.println(res); + } + + class Solution { + public int numSubarrayProductLessThanK(int[] nums, int k) { + + + int res = 0; + int pro = 1; + for (int left = 0, right = 0; right < nums.length; right++) { + pro *= nums[right]; + while (pro >= k) { + // 一旦 找到比k小的,左边就往前,并且pro得到最新的 + pro /= nums[left++]; + } + res += right - left + 1; + + } + + return res; + + } + } + + class Solution1 { + public int numSubarrayProductLessThanK(int[] nums, int k) { + if (k <= 1) return 0; //1 <= nums[i] <= 1000 + int res = 0; + int pro = 1; + + int left = 0; + int right = 0; + while (right < nums.length) { + pro *= nums[right]; + // 直到总乘积大于k,左边才左移 + while (pro >= k) { + pro /= nums[left]; + left++; + } + res += right - left + 1; + right++; + } + return res; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LCR001.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LCR001.java new file mode 100644 index 0000000..0bfe340 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LCR001.java @@ -0,0 +1,135 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 12:16 + * @注释 + */ +public class LCR001 { + + @Test + public void test() { + Solution solution = new Solution(); + int a = -2147483648; + int b = 2; + int res = solution.divide(a, b); + System.out.println(res); + } + + class Solution { + public int divide(int a, int b) { + + //特殊情况1, b=1 + if (b == 1){ + return a; + } + //特殊情况2, b=-1 + if (b == -1){ + return a == Integer.MIN_VALUE ? Integer.MAX_VALUE : -a; + } + //特殊情况3, a=0 + if (a == 0){ + return 0; + } + + /** + * 17/3 + * + * 17 3<<1=6 6<<1=12 12<<1=24 2^2 + * 5 3<<1=6 2^0 + * + */ + + //确定符号 + boolean positive = (a ^ b) >= 0; + //为避免溢出, 转换为负数进行计算 -2^31 <= a, b <= 2^31 - 1 + a = a < 0 ? a : -a; + b = b < 0 ? b : -b; + + + int res = 0; + while (a <= b) { + + int i = b; // 标记被除数的变化,不断x2 + int tmpRes = 1; // 记录包含了多少个b + while (a - i <= i) { + i <<= 1; + tmpRes <<= 1; + } + res += tmpRes; + a = a - i; + } + + return positive ? res : -res; + } + + + } + + class Solution1 { + /** + * 没有转为负数处理会错误 + * 输入 + * a =-2147483648 + * b =2 + * 输出 + * 0 + * 预期结果 + * -1073741824 + * @param a + * @param b + * @return + */ + public int divide(int a, int b) { + + //特殊情况1, b=1 + if (b == 1){ + return a; + } + //特殊情况2, b=-1 + if (b == -1){ + return a == Integer.MIN_VALUE ? Integer.MAX_VALUE : -a; + } + //特殊情况3, a=0 + if (a == 0){ + return 0; + } + + /** + * 17/3 + * + * 17 3<<1=6 6<<1=12 12<<1=24 2^2 + * 5 3<<1=6 2^0 + * + */ + + //确定符号 + boolean positive = (a ^ b) >= 0; + //为避免溢出, 转换为负数进行计算 -2^31 <= a, b <= 2^31 - 1 + a = a < 0 ? -a : a; + b = b < 0 ? -b : b; + + + int res = 0; + while (a >= b) { + + int i = b; // 标记被除数的变化,不断x2 + int tmpRes = 1; // 记录包含了多少个b + while (a - i >= i) { + i <<= 1; + tmpRes <<= 1; + } + res += tmpRes; + a = a - i; + } + + return positive ? res : -res; + } + + + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LCR090.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LCR090.java new file mode 100644 index 0000000..a4e2cf2 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LCR090.java @@ -0,0 +1,54 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 12:55 + * @注释 + */ +public class LCR090 { + + + @Test + public void test() { + int[] nums = {1,7,9,2}; + System.out.println(new Solution().rob(nums)); + } + + class Solution { + public int rob(int[] nums) { + if (nums.length == 0) { + return 0; + } + if (nums.length == 1) { + return nums[0]; + } + if (nums.length == 2) { + return Math.max(nums[0], nums[1]); + } + + //如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。 + //环形街道 + return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1)); + } + + public int rob(int[] nums, int start, int end) { + int dp[] = new int[nums.length]; + // dp【i】表示 到i最高能偷窃的个数 + // dp[start]=1 + // dp[start+1]=1 + // dp[i] = max(dp[i-2]+nums[i],dp[i-1]) + dp[start] = nums[start]; + dp[start + 1] = Math.max(nums[start], nums[start + 1]); + for (int i = start + 2; i <= end; i++) { + + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[end]; + + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode005.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode005.java new file mode 100644 index 0000000..490f632 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode005.java @@ -0,0 +1,92 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 13:16 + * @注释 + */ +public class LeetCode005 { + + @Test + public void test() { + String s = "babad"; + String res = new Solution1().longestPalindrome(s); + System.out.println(res); + } + + class Solution { + /** + * 确定中间节点,不断向外扩展,判断有几个回文 + * - 中间为1个字符 + * - 中间为2个字符 + * @param s + * @return + */ + public String longestPalindrome(String s) { + + char[] chars = s.toCharArray(); + String res = ""; + for (int i = 0; i < chars.length; i++) { + String index1 = getHuiWenByIndex(chars, i, i); + if (index1.length() > res.length()) { + res = index1; + } + String index2 = getHuiWenByIndex(chars, i, i + 1); + if (index2.length() > res.length()) { + res = index2; + } + } + return res; + } + + public String getHuiWenByIndex(char[] chars, int left,int right) { + while (left >= 0 && right < chars.length && chars[left] == chars[right]) { + left--; + right++; + } + return new String(chars, left + 1, right - left - 1); + } + } + + class Solution1 { + /** + * 动态规划 + * @param s + * @return + */ + public String longestPalindrome(String s) { + /** + * dp[i][j] 表示i-j是否为回文串 + * dp[i][j] = + * s[i] == s[j] + * 1. i=j true + * 2. i=j+1 true + * 3. dp[i+1][j-1] 确定了遍历顺序 从下往上,从左往右 + */ + int length = s.length(); + boolean[][] dp = new boolean[length][length]; + + int left = 0; + int right = 0; + for (int i = length - 1; i >= 0; i--) { + for (int j = i; j < length; j++) { + if (s.charAt(i) == s.charAt(j)) { + if (i == j || i == j - 1 || dp[i + 1][j - 1]) { + if (right - left <= j - i) { + left = i; + right = j; + } + dp[i][j] = true; + } + } + } + } + + + return s.substring(left, right + 1); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode1049.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode1049.java new file mode 100644 index 0000000..63e32f7 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode1049.java @@ -0,0 +1,79 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 15:09 + * @注释 + */ +public class LeetCode1049 { + + + @Test + public void test() + { + Solution solution = new Solution(); + int[] stones = new int[]{31,26,33,21,40}; + int i = solution.lastStoneWeightII(stones); + System.out.println(i); + } + + class Solution { + /** + * 背包问题 + * + * dp[i][j] 表示0-i物品里选择 放入j背包 + * + * 石头的总重量的一半为目标,找到几块石头可以尽可能满足half + * + * + * + * @param stones + * @return + */ + public int lastStoneWeightII(int[] stones) { + + int sum = 0; + for (int stone : stones) { + sum += stone; + } + + int half = sum / 2; //背包容量 + int[][] dp = new int[stones.length][half + 1]; + /** + * 2 3 4 5 3 + * + * 0 1 2 3 4 + * 0 0 0 2 2 2 + * 1 + * 2 + * 3 + * 4 + * + * 1. 放入物品i dp[i][j] = dp[i][j-weight[i]]+values[i] + * 2. 不放物品i = dp[i-1][j] + * + */ + + for (int i = stones[0]; i <= half; i++) { + dp[0][i] = stones[0]; + } + + for (int i = 1; i < stones.length; i++) { + for (int j = 1; j <= half; j++) { + if (stones[i] > j) { + // 放不下 + dp[i][j] = dp[i - 1][j]; + }else { + dp[i][j] = Math.max(dp[i - 1][j - stones[i]] + stones[i], dp[i - 1][j]); + } + } + + } + + return sum - 2 * dp[stones.length - 1][half]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode343.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode343.java new file mode 100644 index 0000000..c564792 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode343.java @@ -0,0 +1,52 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 17:09 + * @注释 + */ +public class LeetCode343 { + + @Test + public void test() + { + Solution solution = new Solution(); + int i = solution.integerBreak(10); + System.out.println(i); + } + + class Solution { + /** + * dp[j] 表示 j拆分出来的的最大乘积的最大值 + * - dp[j] + * j * i + * dp[j-i]+dp[i] 就是对j再进行拆分 + * + * 10 + * dp + * 0 1 2 3 4 5 6 7 8 9 10 + * 9 10 21 24 25 25 + * @param n + * @return + */ + public int integerBreak(int n) { + + int[] dp = new int[n + 1]; + dp[0] = 0; + dp[1] = 1; + dp[2] = 1; + for (int i = 3; i <= n; i++) { + for (int j = 1; j < i; j++) { + int max = Math.max((i - j) * j, dp[i - j] * j); + dp[i] = Math.max(dp[i], max); + } + } + + return dp[n]; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode416.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode416.java new file mode 100644 index 0000000..93b4edc --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode416.java @@ -0,0 +1,100 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 18:22 + * @注释 + */ +public class LeetCode416 { + + + @Test + public void test() { + int[] nums = {1,6,5}; + boolean canPartition = new Solution().canPartition(nums); + System.out.println(canPartition); + } + + class Solution { + /** + * 把n个数放入到背包中 + * dp[j] 为0-i中任取放入容量为j的背包的最大价值,让价值等于nums总数的一半 + * + * @param nums + * @return + */ + public boolean canPartition(int[] nums) { + int sum = 0; + for (int num : nums) { + sum += num; + } + if (sum % 2 == 1) { + return false; + } + int half = sum / 2; + + int[] dp = new int[half + 1]; + + + /** + * 内层循环从容量的一半开始,逐渐减小,直到小于当前物品的重量。 + */ + for (int i = 0; i < nums.length; i++) { + for (int j = half; j >= nums[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); + + if(dp[half] == half) return true; + } + } + + + + return dp[half] == half; + } + } + + class Solution1 { + /** + * 把n个数放入到背包中 + * dp[i][j] 为0-i中任取放入容量为j的背包的最大价值,让价值等于nums总数的一半 + * + * @param nums + * @return + */ + public boolean canPartition(int[] nums) { + + + int sum = 0; + for (int num : nums) { + sum += num; + } + if (sum % 2 == 1) { + return false; + } + int half = sum / 2; + + int[][] dp = new int[nums.length][half + 1]; + for (int i = nums[0]; i < half + 1; i++) { + dp[0][i] = nums[0]; + } + + + for (int i = 1; i < nums.length; i++) { + for (int j = 1; j <= half; j++) { + if (j < nums[i]) { + dp[i][j] = dp[i - 1][j]; + }else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); + } + } + } + + return dp[nums.length - 1][half] == half; + } + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode45.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode45.java new file mode 100644 index 0000000..d0139b2 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode45.java @@ -0,0 +1,48 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 16:14 + * @注释 + */ +public class LeetCode45 { + + @Test + public void test() + { + Solution solution = new Solution(); + int[] nums = {2,3,1,1,4}; + int jump = solution.jump(nums); + System.out.println(jump); + } + + class Solution { + public int jump(int[] nums) { + if (nums.length == 1) { + return 0; + } + + int maxCover = 0; + int curCover = 0; // 当前区间 + + int jump = 0; + for (int i = 0; i < nums.length; i++) { + maxCover = Math.max(maxCover, i + nums[i]); + // 当遍历到达当前覆盖范围的边界时,更新当前覆盖范围的起始位置,并增加一次跳跃次数 + if (i == curCover) { + curCover = maxCover; + jump++; + } + // 如果当前覆盖范围已经到达或超过数组最后一个位置,则返回跳跃次数 + if (curCover >= nums.length - 1) { + return jump; + } + } + return jump; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode647.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode647.java new file mode 100644 index 0000000..6f9b063 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode647.java @@ -0,0 +1,84 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 14:02 + * @注释 + */ +public class LeetCode647 { + + @Test + public void test() + { + System.out.println(new Solution1().countSubstrings("aaa")); + } + + + class Solution { + public int countSubstrings(String s) { + + char[] chars = s.toCharArray(); + int res = 0; + for (int i = 0; i < chars.length; i++) { + res += getHuiWenCount(chars, i, i); + res += getHuiWenCount(chars, i, i + 1); + + } + return res; + } + + /** + * + * @param chars + * @param left + * @param right + * @return left 和 right 为中心的回文串的个数 + */ + public int getHuiWenCount(char[] chars, int left, int right) { + int res = 0; + while (left >= 0 && right < chars.length && chars[left] == chars[right]) { + res++; + left--; + right++; + } + return res; + } + } + + class Solution1 { + /** + * dp[i][j] 表示i-j内回文的个数 + * + * s[i]==s[j] + * 1. i=j 1 + * 2. i=j-1 2 + * 3. dp[i+1][j-1] + 1 从下往上,左到右遍历 + * + * @param s + * @return + */ + public int countSubstrings(String s) { + char[] chars = s.toCharArray(); + int length = chars.length; + boolean[][] dp = new boolean[chars.length][chars.length]; + + int res = 0; + for (int i = length - 1; i >= 0; i--) { + for (int j = i; j < length; j++) { + if (chars[i] == chars[j]) { + if (j - i == 1 || i == j || dp[i + 1][j - 1]) { + dp[i][j] = true; + res++; + } + } + } + } + + return res; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode713.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode713.java new file mode 100644 index 0000000..900d0f7 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode713.java @@ -0,0 +1,45 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 14:33 + * @注释 + */ +public class LeetCode713 { + + @Test + public void test() + { + int[] nums = {10,5,2,6}; + int k = 100; + int res = new Solution().numSubarrayProductLessThanK(nums,k); + System.out.println(res); + } + + class Solution { + public int numSubarrayProductLessThanK(int[] nums, int k) { + if (k == 0) { + return 0; + } + + int left = 0; + int right = 0; + int pro = 1; + int res = 0; + while (right < nums.length) { + pro *= nums[right]; + while (pro >= k && left <= right) { // 1,1,1 1的时候,但也可以修改 if (k <= 1) return 0; 因为 1 <= nums[i] <= 1000 + pro /= nums[left]; + left++; + } + res += right - left + 1; + right++; + } + + return res; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode738.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode738.java new file mode 100644 index 0000000..027e7e4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode738.java @@ -0,0 +1,44 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 15:43 + * @注释 + */ +public class LeetCode738 { + + @Test + public void test() { + int n =10; + Solution solution = new Solution(); + System.out.println(solution.monotoneIncreasingDigits(n)); + } + + class Solution { + public int monotoneIncreasingDigits(int n) { + String s = String.valueOf(n); + char[] chars = s.toCharArray(); + /** + * 98 + * 89 + */ + int index = chars.length; + for (int i = chars.length - 1; i > 0; i--) { + if (chars[i] < chars[i - 1]) { + chars[i - 1] -= 1; + index = i-1; + } + } + + for (int i = index + 1; i < chars.length; i++) { + chars[i] = '9'; + } + + return Integer.parseInt(new String(chars)); + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode96.java b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode96.java new file mode 100644 index 0000000..8ccb60f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_24/LeetCode96.java @@ -0,0 +1,62 @@ +package cn.whaifree.redo.redo_24_3_24; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/24 16:57 + * @注释 + */ +public class LeetCode96 { + + @Test + public void test() + { + Solution solution = new Solution(); + int i = solution.numTrees(3); + System.out.println(i); + } + + class Solution { + public int numTrees(int n) { + if (n <= 2) { + return n; + } + + /** + * + * 二叉搜索树 + * + * dp[i] 表示i颗树有的可能性 + * dp[0] = 1 + * dp[1] = 2 + * + * n = 3 + * 以1为头 以2为头 以3为头 + * dp[0] * dp[2] + + * dp[1] * dp[1] + + * dp[2] * dp[0] + * + * n + * dp[i] * dp[n-i-1] + * .... + * dp[n-i-1] * dp[i] + */ + + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i < dp.length; i++) { + int sum = 0; + for (int j = 0; j < i; j++) { + sum += (dp[i - j - 1] * dp[j]); + } + dp[i] = sum; + } + + return dp[n]; + } + } +} diff --git a/src/main/java/cn/whaifree/test/MT.java b/src/main/java/cn/whaifree/test/MT.java new file mode 100644 index 0000000..7dff467 --- /dev/null +++ b/src/main/java/cn/whaifree/test/MT.java @@ -0,0 +1,138 @@ +package cn.whaifree.test; + +import java.util.HashSet; +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/23 11:09 + * @注释 + */ +public class MT { + + + + +// public static void main(String[] args) { +// +// Scanner in = new Scanner(System.in); +// +// String[] s = in.nextLine().split(" "); +// +// int a = Integer.parseInt(s[0]); +// int b = Integer.parseInt(s[1]); +// int[][] ints = new int[a][b]; +// for(int i = 0 ; i0;number--){ + sb.append(s[i]); + } + } + + System.out.println(res(sb.toString().toCharArray(), k, 0)); + + + + + } + + public static int res(char[] chars,int k,int start){ + + if(start>=chars.length){ + return 0; + } + + int weight = 0; + HashSet set = new HashSet<>(); + int i; + for(i = start ; weight