leecode更新
This commit is contained in:
parent
764aae3459
commit
d999abe289
|
|
@ -58,7 +58,7 @@ public class T03_LengthOfLongestSubstring {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 官方最快:使用数组来代替map
|
* 官方最快:使用数组来代替map
|
||||||
* 速度击败1005 内存击败68.17% 1ms
|
* 速度击败100% 内存击败68.17% 1ms
|
||||||
* @param s
|
* @param s
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.markilue.leecode.sort;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue