From 6878c313228aa7f9fc627c97a3f446f4f9fef753 Mon Sep 17 00:00:00 2001 From: kyriewhluo Date: Thu, 29 Aug 2024 20:02:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(leetcodes):=20=E6=B7=BB=E5=8A=A0LR181?= =?UTF-8?q?=E5=92=8CLeetCode215=E9=A2=98=E7=9B=AE=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现LR181字符串反转逻辑,通过调整空格位置反转单词顺序。 - 添加LeetCode215,采用堆排序算法查找第k大的数字,包含优化的小顶堆实现。- 扩展TreeNode类以支持通过整数数组构建二叉树的功能。 --- .../whaifree/leetCode/Array/LeetCode215.java | 102 ++++++++++++++++++ .../cn/whaifree/leetCode/String/LCR181.java | 46 ++++++++ .../cn/whaifree/leetCode/model/TreeNode.java | 16 +++ 3 files changed, 164 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Array/LeetCode215.java create mode 100644 src/main/java/cn/whaifree/leetCode/String/LCR181.java diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode215.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode215.java new file mode 100644 index 0000000..8af6b16 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode215.java @@ -0,0 +1,102 @@ +package cn.whaifree.leetCode.Array; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.PriorityQueue; + +public class LeetCode215 { + + @Test + public void main() + { + int[] nums = {3,2,1,5,6,4}; + int k = 2; + Solution solution = new Solution(); + int i = solution.findKthLargest(nums, k); + System.out.println(i); + } + + class Solution { + public int findKthLargest(int[] nums, int k) { + + + // 小顶堆 + PriorityQueue priorityQueue = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + for (int num : nums) { + priorityQueue.offer(num); + if (priorityQueue.size() > k) { + priorityQueue.poll(); + } + } + + return priorityQueue.poll(); + } + } + + @Test + public void test() + { + int[] nums = {3,2,1,5,6,4}; + sort(nums); + } + + /** + * 所有非叶子节点x(自 n/2 开始,表示下面都是叶子节点)找到子节点中的最大值,如果比x 还大,swap。再排序下一个非叶子节点 + */ + public void sort(int[] nums) { + sort(nums, nums.length - 1); + System.out.println(Arrays.toString(nums)); + } + + public void sort(int[] nums, int end) { + if (end < 0) { + return; + } + + int nonLeaf = (end) / 2; + while (nonLeaf >= 0) { + + TreeNode.constructTreeByArrayWithInteger(nums).printTree(); + int left = 2 * nonLeaf + 1; + int right = 2 * nonLeaf + 2; + + if (left <= end &&right <= end) { + if (nums[left] < nums[right]) { + if (nums[right] > nums[nonLeaf]) { + swap(nums, nonLeaf, right); + } + }else { + if (nums[left] > nums[nonLeaf]) { + swap(nums, nonLeaf, left); + } + } + } else if (left <= end) { + if (nums[left] < nums[right]) { + if (nums[right] > nums[nonLeaf]) { + swap(nums, nonLeaf, right); + } + } + }else if (right <= end){ + + } + + nonLeaf--; + } + swap(nums, 0, end); + sort(nums, end - 1); + } + + public void swap(int[] nums, int start, int end) { + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LCR181.java b/src/main/java/cn/whaifree/leetCode/String/LCR181.java new file mode 100644 index 0000000..76fc207 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LCR181.java @@ -0,0 +1,46 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +public class LCR181 { + + + @Test + public void main() + { + Solution solution = new Solution(); + String s = solution.reverseMessage(" abx "); + System.out.println(s); + } + + class Solution { + public String reverseMessage(String message) { + String trim = message.trim(); + String[] split = trim.split("\s+"); + reverse(split); + StringBuilder stringBuilder = new StringBuilder(); + for (String s : split) { + if (s.isEmpty()) { + continue; + } + stringBuilder.append(s).append(" "); + } + if (!stringBuilder.isEmpty()) stringBuilder.deleteCharAt(stringBuilder.length() - 1); + return stringBuilder.toString(); + } + + public void reverse(String[] strings) { + int start = 0; + int end = strings.length - 1; + while (start < end) { + swap(strings, start++, end--); + } + } + + public void swap(String[] strings, int start, int end) { + String temp = strings[start]; + strings[start] = strings[end]; + strings[end] = temp; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java index 06347bd..378ac30 100644 --- a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java @@ -1,6 +1,7 @@ package cn.whaifree.leetCode.model; import java.util.*; +import java.util.function.IntFunction; /** * @version 1.0 @@ -187,6 +188,21 @@ public class TreeNode { return constructTree(is); } + public static TreeNode constructTreeByArrayWithInteger(int... s) { + ArrayList ints = new ArrayList<>(); + for (Integer integer : s) { + ints.add(integer); + } + + Integer[] array = ints.toArray(new IntFunction() { + @Override + public Integer[] apply(int value) { + return new Integer[0]; + } + }); + return constructTree(array); + } + public static TreeNode constructTree(Integer[] array) { if (array == null || array.length == 0 || array[0] == null) { return null;