diff --git a/src/main/java/cn/whaifree/interview/qiuzhao/meituan824.java b/src/main/java/cn/whaifree/interview/qiuzhao/meituan824.java new file mode 100644 index 0000000..99aba10 --- /dev/null +++ b/src/main/java/cn/whaifree/interview/qiuzhao/meituan824.java @@ -0,0 +1,143 @@ +package cn.whaifree.interview.qiuzhao; + +import cn.whaifree.leetCode.model.ListNode; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/24 9:57 + * @注释 + */ +public class meituan824 { + + + +} + +class mtp1{ + + + /** + * 小美初始位于(a,b)位置,二维平面上有n个瓶子,每个瓶子的位置为(x,y),小美每次可以向上、下、左、石移动一格,每次移动的代价为 1, + * 小美需要每次移动到一个瓶子的位置上,然后拿起瓶子把它放到(c,d) 位置,每次最多只能拿一个瓶子。请问最少需要多少代价才能把所有瓶子都放到(c,d)位置上。 + * @param args + */ + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + // 注意 hasNext 和 hasNextLine 的区别 + int startX = in.nextInt(); + int startY = in.nextInt(); + int targetX = in.nextInt(); + int targetY = in.nextInt(); + int num = in.nextInt(); + int res = 0; + for (int i = 0; i < num; i++) { + int x = in.nextInt(); + int y = in.nextInt(); + res += go(startX, startY, x, y); + res += go(x, y, targetX, targetY); + startX = targetX; + startY = targetY; + } + System.out.println(res); + + } + + public static int go(int startX, int startY, int endX, int endY) { + int res = 0; + res += Math.abs(endX - startX); + res += Math.abs(endY - startY); + return res; + } + +} + +class mtp2{ + + + /** + * 小美有三个数字a,b,c,他每次操作可以选择一个数字将其加一k次,小美想知道a×bxc的最大值是多少? + * @param nums + * @return + */ + public static int findMin(int[] nums) { + int min = 0; + for (int i = 1; i < nums.length; i++) { + if (nums[i] < nums[min]) { + min = i; + } + } + return min; + } + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + // 注意 hasNext 和 hasNextLine 的区别 + int[] nums = new int[3]; + for (int i = 0; i < nums.length; i++) { + nums[i] = in.nextInt(); + } + int k = in.nextInt(); + + + for (int i = 0; i < k; i++) { + int min = findMin(nums); + nums[min] += 1; + } + + + System.out.println(((long) nums[0] * nums[1] * nums[2])%1000000007); + +// int tmpMax = 1; +// for (int i = 0; i < nums.length; i++) { +// tmpMax *= nums[i]; +// } +// +// for (int j = 0; j < k; j++) { +// int tmp = tmpMax; +// int indexIncr = 0; +// for (int i = 0; i < nums.length; i++) { +// int num = nums[i]; +// int b = (tmpMax / num) * (num + 1); +// if (tmp < b) { +// tmp = b; +// indexIncr = i; +// } +// } +// tmpMax = Math.max(tmpMax, tmp); +// nums[indexIncr] += 1; +// } +// +// System.out.println(tmpMax); + + } +} + +class test { + + public static void main(String[] args) { + ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5, 6}); + ListNode.printList(rm(listNode, 6)); + } + + + public static ListNode rm(ListNode head, int n) { + ListNode pre = new ListNode(-1, head); + ListNode after = pre; + for (int i = 0; i < n; i++) { + after = after.next; + } + ListNode preTmp = pre; + + while (after.next != null) { + after = after.next; + preTmp = preTmp.next; + } + + preTmp.next = preTmp.next.next; + + return pre.next; + } +} diff --git a/src/main/java/cn/whaifree/redo/redo/redo_24_4_1/LeetCode494.java b/src/main/java/cn/whaifree/redo/redo/redo_24_4_1/LeetCode494.java index ee58888..9ee0534 100644 --- a/src/main/java/cn/whaifree/redo/redo/redo_24_4_1/LeetCode494.java +++ b/src/main/java/cn/whaifree/redo/redo/redo_24_4_1/LeetCode494.java @@ -14,8 +14,8 @@ public class LeetCode494 { public void test() { Solution solution = new Solution(); - int[] nums = new int[]{1, 1, 1, 1, 1}; - int target = 3; + int[] nums = new int[]{1,0}; + int target = 1; int i = solution.findTargetSumWays(nums, target); System.out.println(i); } diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode11.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode11.java new file mode 100644 index 0000000..49bcef4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode11.java @@ -0,0 +1,45 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/24 2:14 + * @注释 + */ +public class LeetCode11 { + + @Test + public void test() { + int[] height = {1,1}; + System.out.println(new Solution().maxArea(height)); + } + + class Solution { + /** + * + * 不断移动短线 + * + * @param height + * @return + */ + public int maxArea(int[] height) { + int left = 0; + int right = height.length - 1; + + int cap = Integer.MIN_VALUE; + + while (left <= right) { + cap = Math.max(cap, (right - left) * Math.min(height[right], height[left])); + if (height[left] < height[right]) { + left++; + }else { + right--; + } + } + return cap; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode121.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode121.java new file mode 100644 index 0000000..7a37d78 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode121.java @@ -0,0 +1,70 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 15:09 + * @注释 + */ +public class LeetCode121 { + @Test + public void test() { + + Solution1 solution = new Solution1(); + int i = solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4}); + System.out.println(i); + + } + + + class Solution { + public int maxProfit(int[] prices) { + + int maxProfit = 0; + int left = 0; + int right = 0; + while (right < prices.length) { + if (prices[right] < prices[left]) { + left = right; + } + maxProfit = Math.max(maxProfit, prices[right] - prices[left]); + right++; + } + return maxProfit; + } + } + + class Solution1 { + /** + * + * 只能选择 某一天 买入这只股票 + * + * + * dp[0][i] 表示第i天手头没有有股票的最大利润 + * - 第i-1天就没有 dp[0][i-1] + * - 今天刚刚卖出 dp[1][i-1] + price[i] + * dp[1][i] 表示第i天手头有股票的最大利润 + * - 前一天就有 dp[1][i-1] + * - 刚刚买入 - price[i] 只能选择 某一天 买入这只股票 + * + * + * + * @param prices + * @return + */ + public int maxProfit(int[] prices) { + + int[][] dp = new int[2][prices.length]; + + dp[0][0] = 0; + dp[1][0] = -prices[0]; + for (int i = 1; i < prices.length; i++) { + dp[0][i] = Math.max(dp[0][i - 1], dp[1][i - 1] + prices[i]); + dp[1][i] = Math.max(dp[1][i - 1], - prices[i]); + } + return dp[0][prices.length - 1]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode21.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode21.java new file mode 100644 index 0000000..75b0271 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode21.java @@ -0,0 +1,54 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/24 2:26 + * @注释 + */ +public class LeetCode21 { + + @Test + public void test() { + + new Solution().mergeTwoLists( + ListNode.listNodeFromArray(new int[]{1, 2, 4}), + ListNode.listNodeFromArray(new int[]{1, 3, 4}) + ).printList(); + } + + class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode tmpHead = new ListNode(-1); + ListNode index1 = list1; + ListNode index2 = list2; + ListNode index = tmpHead; + while (index1 != null && index2 != null) { + if (index1.val < index2.val) { + index.next = index1; + index1 = index1.next; + }else { + index.next = index2; + index2 = index2.next; + } + index = index.next; + } + + while (index1 != null) { + index.next = index1; + index1 = index1.next; + index = index.next; + } + while (index2 != null) { + index.next = index2; + index2 = index2.next; + index = index.next; + } + return tmpHead.next; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode22.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode22.java new file mode 100644 index 0000000..82300ff --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode22.java @@ -0,0 +1,74 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/24 2:35 + * @注释 + */ +public class LeetCode22 { + + @Test + public void test() { + + System.out.println(new Solution().generateParenthesis(1)); + + } + + class Solution { + List res; + StringBuilder path; + Character[] characters; + + public List generateParenthesis(int n) { + +// characters = new Character[n * 2]; +// for (int i = 0; i < n; i++) { +// characters[i] = '('; +// } +// for (int i = n; i < n * 2; i++) { +// characters[i] = ')'; +// } + + res = new ArrayList<>(); + path = new StringBuilder(); + circle(n, n); + return res; + } + + /** + * + * + * @param left + * @param right + */ + + public void circle(int left, int right) { + if (left == 0 && right == 0) { + res.add(path.toString()); + return; + } + if (left < 0) { + return; + } + if (left > right) { + return; + } + + path.append("("); + circle(left - 1, right); + path.deleteCharAt(path.length() - 1); + + path.append(")"); + circle(left, right - 1); // 回溯后左边括号还是可以用的 + path.deleteCharAt(path.length() - 1); + + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode23.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode23.java new file mode 100644 index 0000000..fc04782 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode23.java @@ -0,0 +1,58 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +import java.util.Comparator; +import java.util.PriorityQueue; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 0:51 + * @注释 + */ +public class LeetCode23 { + + @Test + public void test() { + + new Solution().mergeKLists( + new ListNode[]{ + // [[-2,-1,-1,-1],[]] + ListNode.listNodeFromArray(new int[]{-2,-1,-1,-1}), + ListNode.listNodeFromArray(new int[]{}), + } + ).printList(); + + } + + class Solution { + public ListNode mergeKLists(ListNode[] lists) { + PriorityQueue priorityQueue = new PriorityQueue<>(new Comparator() { + @Override + public int compare(ListNode o1, ListNode o2) { + return o1.val - o2.val; + } + }); + + for (ListNode list : lists) { + ListNode tmp = list; + while (tmp != null) { + priorityQueue.add(tmp); + tmp = tmp.next; + } + } + + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + while (!priorityQueue.isEmpty()) { + ListNode poll = priorityQueue.poll(); + poll.next = null; // 防止出现循环 + tmp.next = poll; + tmp = tmp.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode2602.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode2602.java new file mode 100644 index 0000000..82a35d5 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode2602.java @@ -0,0 +1,82 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/22 23:09 + * @注释 + */ +public class LeetCode2602 { + + class Solution { + public List minOperations(int[] nums, int[] queries) { + List res = new ArrayList<>(); + for (int query : queries) { + res.add(minOpr(nums, query)); + } + return res; + } + + public long minOpr(int[] nums, long q) { + long res = 0; + for (int num : nums) { + res += Math.abs(num - q); + } + return res; + } + } + + @Test + public void test() { + + // [3,1,6,8], queries = [1,5] + int[] nums = new int[]{3, 1, 6, 8}; + int[] queries = new int[]{1, 5}; + Solution1 solution = new Solution1(); + List res = solution.minOperations(nums, queries); + System.out.println(res); + } + + class Solution1 { + public List minOperations(int[] nums, int[] queries) { + Arrays.sort(nums); + + long[] preSum = new long[nums.length + 1]; + for (int i = 1; i <= nums.length; i++) { + preSum[i] = preSum[i - 1] + nums[i - 1]; + } + + + + List res = new ArrayList<>(); + for (int query : queries) { + res.add(minOpr(preSum, nums, query)); + } + return res; + } + + public long minOpr(long[] preSum,int[] nums, int q) { + int index = Arrays.binarySearch(nums, q); + if (index < 0) { + // index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The + index = -index - 1; + } + + long preSize = preSum[index]; + long then = (long) q * index - preSize; + long square = (long)(preSum.length - index - 1) * q; + long up = preSum[preSum.length - 1] - preSize - square; + + return then + up; + } + + + + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode29.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode29.java new file mode 100644 index 0000000..9c236bd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode29.java @@ -0,0 +1,68 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 14:45 + * @注释 + */ +public class LeetCode29 { + + @Test + public void test(){ + + Solution solution = new Solution(); + int i = solution.divide(-14,7 ); + System.out.println(i); + + } + + class Solution { + + /** + * + * 17/3 3<2 12 5 3<0 + * + * 2^2 + 2^0 = 5 + * + * + * 10 / 3 + * + * 3<1 6 4 3<0 + * + * 2^1+2^0 + * + * @param dividend + * @param divisor + * @return + */ + public int divide(int dividend, int divisor) { + + + if (dividend == 0) { + return 0; + } + if (dividend == Integer.MIN_VALUE && divisor == -1) { + return Integer.MAX_VALUE; + } + boolean negative; + negative = (dividend ^ divisor) <0;//用异或来计算是否符号相异 + + long Ldividend = Math.abs((long) dividend); + long Ldivisor = Math.abs((long) divisor); + + int sumPow = 0; + while (Ldividend >= Ldivisor) { + int pow = 0; + while ((Ldivisor << (pow + 1)) < Ldividend) { + pow++; + } + Ldividend -= Ldivisor << pow; + sumPow += (int) Math.pow(2, pow); + } + return negative ? -sumPow : sumPow; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode31.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode31.java new file mode 100644 index 0000000..ee68784 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode31.java @@ -0,0 +1,65 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 1:09 + * @注释 + */ +public class LeetCode31 { + + @Test + public void test() { + // 123546321 + // 找到递增,在右边找到能够让其变大的最靠右的(此时右边部分一定递减),替换,逆转右半部分 + int[] nums = {1, 2, 3, 5, 4, 6,5,2,1}; + new Solution().nextPermutation(nums); + for (int num : nums) { + System.out.print(num); + } + } + + class Solution { + public void nextPermutation(int[] nums) { + // 123465 下一个排列 123546321 + // 从后往前找到第一个升序的[a,x1,x2,b] + // 从b那部分找到第一个大于x1的c + // 替换x1 和c + // reverse x2-- + int i; + for (i = nums.length - 2; i >= 0; i--) { + if (nums[i] < nums[i + 1]) { + break; + } + } + if (i == -1) { + reverse(nums, 0, nums.length - 1); + return; + } + int j; + for (j = nums.length - 1; j > i; j--) { + if (nums[j] > nums[i]) { + break; + } + } + swap(nums, i, j); + reverse(nums, i + 1, nums.length - 1); + } + + public void reverse(int[] nums, int start, int end) { + while (start < end) { + swap(nums, start, end); + start++; + end--; + } + } + + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode32.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode32.java new file mode 100644 index 0000000..0451695 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode32.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 14:22 + * @注释 + */ +public class LeetCode32 { + + @Test + public void test(){ + String s = "()(()"; + Solution solution = new Solution(); + int i = solution.longestValidParentheses(s); + System.out.println(i); + + } + + class Solution { + public int longestValidParentheses(String s) { + char[] charArray = s.toCharArray(); + Deque stack = new LinkedList<>(); + + + boolean[] dp = new boolean[s.length()]; + for (int i = 0; i < charArray.length; i++) { + char c = charArray[i]; + if (c == '(') { + stack.push(i); + }else if (!stack.isEmpty()){ + Integer pop = stack.pop(); + dp[pop] = true; + dp[i] = true; + } + } + + // 计算flag中最长连续出现1的次数 + int len = 0; + int maxLen = Integer.MIN_VALUE; + for (int i = 0; i < dp.length; i++) { + if (dp[i]) { + len += 1; + }else { + maxLen = Math.max(maxLen,len); + len = 0; + } + } + + return Math.max(maxLen, len); // (() 这个用例 + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode337.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode337.java new file mode 100644 index 0000000..e9d4f53 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode337.java @@ -0,0 +1,48 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 15:45 + * @注释 + */ +public class LeetCode337 { + + @Test + public void test() { + System.out.println( + new Solution() + .rob(TreeNode.constructTree(new Integer[]{3, 2, 3, null, 3, null, 1}))); + + } + + + class Solution { + public int rob(TreeNode root) { + int[] ints = robOrNot(root); + return Math.max(ints[0], ints[1]); + } + + public int[] robOrNot(TreeNode root) { + + if (root == null) { + return new int[2]; + } + + int[] res = new int[2]; + + int[] right = robOrNot(root.right); + int[] left = robOrNot(root.left); + + res[0] = Math.max(right[0], right[1]) + Math.max(left[0], left[1]); + res[1] = right[0] + left[0] + root.val; + + return res; + } + + + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode42.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode42.java new file mode 100644 index 0000000..e5aab4a --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode42.java @@ -0,0 +1,47 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 17:29 + * @注释 + */ +public class LeetCode42 { + + @Test + public void test(){ + + System.out.println(new Solution().trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1})); + } + + class Solution { + public int trap(int[] height) { + int[] leftHeight = new int[height.length]; + + int tmpLeftHeight = 0; + for (int i = 0; i < height.length; i++) { + int n = height[i]; + tmpLeftHeight = Math.max(tmpLeftHeight, n); + leftHeight[i] = tmpLeftHeight; + } + int[] rightHeight = new int[height.length]; + int tmpRightHeight = 0; + for (int i = height.length - 1; i >= 0; i--) { + int n = height[i]; + tmpRightHeight = Math.max(tmpRightHeight, n); + rightHeight[ i ] = tmpRightHeight; + } + + int res = 0; + for (int i = 0; i < height.length; i++) { + int left = leftHeight[i]; + int right = rightHeight[i]; + res += (Math.min(left, right) - height[i]); + } + + return res; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode474.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode474.java new file mode 100644 index 0000000..49d0d7f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode474.java @@ -0,0 +1,133 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @Author whai文海 + * @Date 2024/8/22 21:00 + * @version 1.0 + * @注释 + * + */ +public class LeetCode474 { + + @Test + public void test() { + String[] strs = {"10", "0001", "111001", "1", "0"}; + int m = 5; + int n = 3; + Solution solution = new Solution(); + System.out.println(solution.findMaxForm(strs, m, n)); + } + + class Solution { + /** + * + * 物品 ["10", "0001", "111001", "1", "0"] + * + * 背包 + * + * dp[i][j][k] 表示在前i个字符串中,放入了j个0和k个1的背包最大子集长度 + * 物品 i + * 0 j + * 1 k + * + * + * + * + * + * + * + * @param strs + * @param m + * @param n + * @return + */ + public int findMaxForm(String[] strs, int m, int n) { + + int[][][] dp = new int[strs.length][m + 1][n + 1]; + + String index0 = strs[0]; + int index0Zero = calculateZero(index0); + int index0One = index0.length() - index0Zero; + for (int i = index0Zero; i <= m; i++) { + for (int j = index0One; j <= n; j++) { + dp[0][i][j] = 1; + } + } + + + for (int i = 1; i < strs.length; i++) { + String str = strs[i]; + int zero = calculateZero(str); + int one = str.length() - zero; + for (int j = 0; j <= m; j++) { + for (int k = 0; k <= n; k++) { + if (zero > j || one > k) { + dp[i][j][k] = dp[i - 1][j][k]; + }else { + dp[i][j][k] = Math.max(dp[i - 1][j][k], dp[i - 1][j - zero][k - one] + 1); + } + } + } + } + return dp[strs.length-1][m][n]; + } + + public int calculateZero(String str) { + int zero = 0; + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '0') { + zero++; + } + } + return zero; + } + } + + class Solution1 { + public int findMaxForm(String[] strs, int m, int n) { + + // 三维 dp[i][j][k] 表示在前i个字符串中,包含了了j个0和k个1的最大子集长度 + // String[i]为物品 j k 为背包 + // 初始化: 当物品i=0 ,表示在前0个字符串中,没有字符串可以使用,dp[0][j][k]=0 + // 递推公式: + // 获取0的数量numZero 获取1的数量numOne + // 当j sum) return 0; // 此时没有方案 + if ((sum - target) % 2 == 1) return 0; // 此时没有方案 + + + int[][] dp = new int[nums.length + 1][packageSize + 1]; + for (int i = 0; i < dp.length; i++) { + dp[i][0] = 1; + } + + for (int i = 1; i <= nums.length; i++) { + for (int j = 0; j <= packageSize; j++) { + if (j >= nums[i - 1]) { + dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]]; + }else { + dp[i][j] = dp[i - 1][j]; + } + } + } + + return dp[nums.length][packageSize]; + } + } +} diff --git a/src/main/java/cn/whaifree/tech/spring/IOCDemo.java b/src/main/java/cn/whaifree/tech/spring/IOCDemo.java new file mode 100644 index 0000000..1f83c5b --- /dev/null +++ b/src/main/java/cn/whaifree/tech/spring/IOCDemo.java @@ -0,0 +1,71 @@ +package cn.whaifree.tech.spring; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * + * 1. 容器,能够扫描包获取Bean + * 能够使用反射创建对象 + * 能够DI,反射获取参数并从容器中获取 + * + * 2. 注解 + * + * + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/25 11:16 + * @注释 + */ +public class IOCDemo { + + +} + + + +// 定义容器接口 +interface Container { + T getBean(Class clazz); +} + +// 容器实现类 +class SimpleIoCContainer implements Container { + private final Map, Object> beans = new HashMap<>(); + + @Override + public T getBean(Class clazz) { + return clazz.cast(beans.get(clazz)); + } + + public void registerBean(Class clazz, T instance) { + beans.put(clazz, instance); + } + + +} + +// 示例类 +class Service { + public String hello() { + return "Hello, World!"; + } +} + +class Main { + public static void main(String[] args) { + // 创建容器实例 + SimpleIoCContainer container = new SimpleIoCContainer(); + + // 注册服务 + Service service = new Service(); + container.registerBean(Service.class, service); + + // 从容器中获取服务 + Service retrievedService = container.getBean(Service.class); + + // 使用服务 + System.out.println(retrievedService.hello()); + } +}