diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/MinWindow.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/MinWindow.java new file mode 100644 index 0000000..4129e7c --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/MinWindow.java @@ -0,0 +1,86 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2022 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.algorithm; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author ehlxr + * @since 2022-04-29 11:07. + */ +public class MinWindow { + public static void main(String[] args) { + System.out.println(minWindow("cabwefgewcwaefgcf", "cae")); + System.out.println(minWindow("bba", "ab")); + System.out.println(minWindow("ADOBECODEBANC", "ABC")); + System.out.println(minWindow("a", "a")); + System.out.println(minWindow("a", "b")); + System.out.println(minWindow("", "a")); + System.out.println(minWindow("", "")); + System.out.println(minWindow("a", "aa")); + System.out.println(minWindow("aa", "a")); + } + + public static String minWindow(String s, String t) { + Map windows = new HashMap<>(); + Map needs = new HashMap<>(); + for (int i = 0; i < t.length(); i++) { + needs.put(t.charAt(i), needs.getOrDefault(t.charAt(i), 0) + 1); + } + + int left = 0, right = 0, valid = 0, start = -1, len = Integer.MAX_VALUE; + while (right < s.length()) { + char r = s.charAt(right); + + if (needs.containsKey(r)) { + windows.put(r, windows.getOrDefault(r, 0) + 1); + if (windows.get(r).equals(needs.get(r))) { + valid++; + } + } + right++; + + while (valid == needs.size()) { + if (right - left < len) { + len = right - left; + start = left; + } + + char l = s.charAt(left); + left++; + + if (windows.containsKey(l)) { + if (windows.get(l).equals(needs.get(l))) { + valid--; + } + windows.put(l, windows.get(l) - 1); + } + } + } + + return start > -1 ? s.substring(start, start + len) : ""; + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/Permutation.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/Permutation.java new file mode 100644 index 0000000..ef2bf1b --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/Permutation.java @@ -0,0 +1,70 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2022 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.algorithm; + +import java.util.ArrayList; +import java.util.List; + +/** + * 全排列 + * + * @author ehlxr + * @since 2022-04-20 16:42. + */ +public class Permutation { + static List> res = new ArrayList<>(); + + public static void main(String[] args) { + int[] arr = {1, 2, 3}; + for (List r : permute(arr)) { + for (int i : r) { + System.out.print(i + " "); + } + System.out.println(); + } + } + + public static List> permute(int[] nums) { + List track = new ArrayList<>(); + backtrack(nums, track); + return res; + } + + public static void backtrack(int[] nums, List track) { + // 回溯结束条件 + if (track.size() == nums.length) { + res.add(new ArrayList<>(track)); + return; + } + for (int num : nums) { + if (track.contains(num)) { + continue; + } + track.add(num); + backtrack(nums, track); + track.remove(track.size() - 1); + } + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java index 2014ce6..135d6cf 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java @@ -39,7 +39,7 @@ public class KnapSack { public static void main(String[] args) { System.out.println(knapsack3(new int[]{2, 2, 4, 6, 3}, new int[]{3, 4, 8, 9, 6}, 5, 9)); - System.out.println(knapsack(new int[]{2, 2, 4, 6, 3}, new int[]{3, 4, 8, 9, 6}, 5, 9)); + // System.out.println(knapsack(new int[]{2, 2, 4, 6, 3}, new int[]{3, 4, 8, 9, 6}, 5, 9)); } /** @@ -51,17 +51,17 @@ public class KnapSack { * @param w 背包可承载重量 * @return 最大价值 */ - public static int knapsack(int[] weight, int[] value, int n, int w) { - int[][] dp = new int[][]; - } + // public static int knapsack(int[] weight, int[] value, int n, int w) { + // int[][] dp = new int[][]; + // } /** * 动态规划方式 * * @param weight 物品重量 * @param value 物品的价值 - * @param n 物品个数 - * @param w 背包可承载重量 + * @param n 物品个数 + * @param w 背包可承载重量 * @return 最大价值 */ public static int knapsack3(int[] weight, int[] value, int n, int w) { diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java new file mode 100644 index 0000000..d18ea6c --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java @@ -0,0 +1,95 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2022 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.algorithm.linkedlist; + +/** + * @author ehlxr + * @since 2022-05-01 14:18. + */ +public class FindKthFromEnd { + public static void main(String[] args) { + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + head.next.next.next.next = new ListNode(5); + head.next.next.next.next.next = new ListNode(6); + + System.out.println(head); + + System.out.println(findKthFromEnd(head, 1)); + System.out.println(findKthFromEnd2(head, 1)); + } + + public static ListNode findKthFromEnd(ListNode head, int k) { + int n = 0; + ListNode node = head; + ListNode p = null; + while (node != null) { + if (p != null) { + p = p.next; + } + + n++; + if (n == k) { + p = head; + } + + node = node.next; + } + return p; + } + + public static ListNode findKthFromEnd2(ListNode head, int k) { + ListNode p = head; + ListNode q = head; + for (int i = 0; i < k; i++) { + q = q.next; + } + while (q != null) { + p = p.next; + q = q.next; + } + return p; + } + + + static class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } + + @Override + public String toString() { + return "ListNode{" + + "val=" + val + + ", next=" + next + + '}'; + } + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java new file mode 100644 index 0000000..0367810 --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java @@ -0,0 +1,92 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2022 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.algorithm.linkedlist; + +/** + * @author ehlxr + * @since 2022-05-03 14:26. + */ +public class ReverseKGroup { + public static void main(String[] args) { + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + head.next.next.next.next = new ListNode(5); + System.out.println(head); + System.out.println(reverseKGroup(head, 2)); + } + + public static ListNode reverseKGroup(ListNode head, int k) { + ListNode p = head; + for (int i = 0; i < k; i++) { + if (p == null) { + return head; + } + p = p.next; + } + + ListNode r = reverseN(head, p); + head.next = reverseKGroup(p, k); + + return r; + } + + public static ListNode reverseN(ListNode head, ListNode n) { + ListNode cur = head, pre = null; + while (cur != n) { + ListNode next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + } + return pre; + } + + public static class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } + + @Override + public String toString() { + return "ListNode{" + + "val=" + val + + ", next=" + next + + '}'; + } + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java index 0d6cf30..77cacbf 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java @@ -36,17 +36,53 @@ public class ReverseLinkedList { Node node4 = new Node(4, node5); Node node3 = new Node(3, node4); Node node2 = new Node(2, node3); - Node node1 = new Node(1, node2); - Node node0 = new Node(0, node1); - Node root = new Node(null, node0); + Node head = new Node(1, node2); - Node reverse = reverse(root); - // root.print(); + // Node reverse = reverse(root); + // reverse.print(); - reverse.print(); + // System.out.println(reverseToN(head, node3)); + // System.out.println(reverseToN(head, 2)); + System.out.println(reverseFm2N(head, 2, 4)); } + public static Node reverseToN(Node root, Node n) { + Node pre = null, cur = root, tmp = root; + while (cur != n) { + tmp = cur.next; + + cur.next = pre; + pre = cur; + cur = tmp; + } + + return pre; + + } + + static Node next = null; + + public static Node reverseToN(Node root, int n) { + if (n == 1) { + next = root.next; + return root; + } + + Node h = reverseToN(root.next, n - 1); + root.next.next = root; + root.next = next; + return h; + } + + public static Node reverseFm2N(Node root, int m, int n) { + if (m == 1) { + return reverseToN(root, n); + } + root.next = reverseFm2N(root.next, m - 1, n - 1); + return root; + } + public static Node reverse(Node root) { Node pre = null; @@ -73,17 +109,11 @@ class Node { this.value = value; } - public void print() { - System.out.println(this); - Node tmp = this.next; - while (tmp != null) { - System.out.println(tmp); - tmp = tmp.next; - } - } - @Override public String toString() { - return "Node[value=" + this.value + "]"; + return "Node{" + + "value=" + value + + ", next=" + next + + '}'; } }