Conflicts:
	Leecode/src/main/java/com/markilue/leecode/sort/HeapSort.java
This commit is contained in:
markilue 2023-04-14 10:03:50 +08:00
commit 6d66747962
3 changed files with 169 additions and 8 deletions

View File

@ -58,7 +58,7 @@ public class T03_LengthOfLongestSubstring {
/**
* 官方最快:使用数组来代替map
* 速度击败1005 内存击败68.17% 1ms
* 速度击败100% 内存击败68.17% 1ms
* @param s
* @return
*/

View File

@ -0,0 +1,109 @@
package com.markilue.leecode.listnode.selftry;
import com.markilue.leecode.listnode.ListNode;
import com.markilue.leecode.listnode.ListNodeUtils;
/**
* @BelongsProject: Leecode
* @BelongsPackage: com.markilue.leecode.listnode.selftry
* @Author: marklue
* @CreateTime: 2023/4/1 14:40
* @Description: TODO
* @Version: 1.0
*/
public class RreverseKGroup {
public static void main(String[] args) {
ListNode root = ListNodeUtils.build(new int[]{1, 2, 3, 4, 5});
ListNodeUtils.print(reverseKGroup(root, 2));
}
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null) {
return null;
}
ListNode node = head;
//寻找边界
for (int i = 0; i < k; i++) {
if (node == null) return head;//不够k个
node = node.next;
}
//交换前k个
ListNode newNode = reverseGroup1(head, node);
head.next = reverseKGroup(node, k);
return newNode;
}
public static ListNode reverseGroup(ListNode a, ListNode b) {
if (a == null) {
return a;
}
//交换
ListNode root = new ListNode();
ListNode temp = a;
while (temp != b) {
ListNode temp1 = temp.next;
temp.next = root.next;
root.next = temp;
temp = temp1;
}
return root.next;
}
//自删除式
public static ListNode reverseGroup1(ListNode a, ListNode b) {
if (a == null) {
return a;
}
//交换
ListNode root = null;
ListNode cur = a;
ListNode nxt = a;
while (cur != b) {
nxt = cur.next;
cur.next = root;
root = cur;
cur = nxt;
}
return root;
}
private static volatile RreverseKGroup instance;
private RreverseKGroup() {
}
public static RreverseKGroup getInstance() {
if (instance == null) {
synchronized (RreverseKGroup.class) {
if (instance == null) {
instance = new RreverseKGroup();
}
}
}
return instance;
}
}

View File

@ -1,17 +1,69 @@
package com.markilue.leecode.sort;
import org.junit.Test;
import java.util.Arrays;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.sort
*@Author: markilue
*@CreateTime: 2023-04-11 16:00
*@Description: TODO
*@Version: 1.0
* @BelongsProject: Leecode
* @BelongsPackage: com.markilue.leecode.sort
* @Author: marklue
* @CreateTime: 2023/4/13 10:04
* @Description: TODO
* @Version: 1.0
*/
public class HeapSort {
@Test
public void test(){
int[] nums={1,8,5,6,9,7,2,4,3};
heapSort(nums);
System.out.println(Arrays.toString(nums));
}
public void heapSort(int[] nums) {
int length = nums.length;
for (int i = length / 2 - 1; i >= 0; i--) {
//从第一个非叶子节点开始
heapful(nums, i,nums.length);
}
//交换堆顶元素和最后面的元素
for (int i = length - 1; i > 0; i--) {
int temp = nums[i];
nums[i] = nums[0];
nums[0] = temp;
heapful(nums, 0,i);
}
public void heapSort(){
}
public void heapful(int[] nums, int i,int length) {
int num = nums[i];
for (int j = i * 2 + 1; j < length; j = j * 2 + 1) {
if (j + 1 < length && nums[j] > nums[j + 1]) {
j++;
}
if (nums[j] < num) {
nums[i] = nums[j];
i = j;
} else {
//两个都小直接break
break;
}
}
nums[i] = num;//把对应的位置上填充之前的数
}
public void swap(int[] nums, int i, int j) {
}
}