feat(leetcodes): 添加LR181和LeetCode215题目解决方案

- 实现LR181字符串反转逻辑,通过调整空格位置反转单词顺序。
- 添加LeetCode215,采用堆排序算法查找第k大的数字,包含优化的小顶堆实现。- 扩展TreeNode类以支持通过整数数组构建二叉树的功能。
This commit is contained in:
kyriewhluo 2024-08-29 20:02:32 +08:00
parent f40f87b246
commit 6878c31322
3 changed files with 164 additions and 0 deletions

View File

@ -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<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
@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;
}
}

View File

@ -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;
}
}
}

View File

@ -1,6 +1,7 @@
package cn.whaifree.leetCode.model; package cn.whaifree.leetCode.model;
import java.util.*; import java.util.*;
import java.util.function.IntFunction;
/** /**
* @version 1.0 * @version 1.0
@ -187,6 +188,21 @@ public class TreeNode {
return constructTree(is); return constructTree(is);
} }
public static TreeNode constructTreeByArrayWithInteger(int... s) {
ArrayList<Integer> ints = new ArrayList<>();
for (Integer integer : s) {
ints.add(integer);
}
Integer[] array = ints.toArray(new IntFunction<Integer[]>() {
@Override
public Integer[] apply(int value) {
return new Integer[0];
}
});
return constructTree(array);
}
public static TreeNode constructTree(Integer[] array) { public static TreeNode constructTree(Integer[] array) {
if (array == null || array.length == 0 || array[0] == null) { if (array == null || array.length == 0 || array[0] == null) {
return null; return null;