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;