2024/8/25 LeetCode Hot100 LinkedList以及8.25的每日一题(不会做)
This commit is contained in:
parent
f4fef28d50
commit
12a0861326
|
@ -0,0 +1,116 @@
|
|||
package com.cool.hot100.linkedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/08/25/19:42
|
||||
* DayNumber 2
|
||||
* Hard 2
|
||||
*/
|
||||
public class Num148 {
|
||||
|
||||
|
||||
public ListNode sortList(ListNode head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
int length = 0;
|
||||
ListNode node = head;
|
||||
while (node != null) {
|
||||
length++;
|
||||
node = node.next;
|
||||
}
|
||||
ListNode newHead = new ListNode(0, head);
|
||||
for (int subLength = 1; subLength < length; subLength <<= 1) {
|
||||
ListNode pre = newHead;
|
||||
ListNode currentNode = pre.next;
|
||||
while (currentNode != null) {
|
||||
|
||||
ListNode list1 = currentNode;
|
||||
for (int i = 1; i < subLength && currentNode.next != null; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
ListNode list2 = currentNode.next;
|
||||
currentNode.next = null;
|
||||
currentNode = list2;
|
||||
for (int i = 1; i < subLength && currentNode != null && currentNode.next != null; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
ListNode next = null;
|
||||
if (currentNode != null) {
|
||||
next = currentNode.next;
|
||||
currentNode.next = null;
|
||||
}
|
||||
pre.next= merge(list1, list2);
|
||||
while(pre.next!=null){
|
||||
pre=pre.next;
|
||||
}
|
||||
currentNode=next;
|
||||
}
|
||||
|
||||
}
|
||||
return newHead.next;
|
||||
}
|
||||
|
||||
|
||||
// public ListNode sortList(ListNode head) {
|
||||
// return sortList(head, null);
|
||||
// }
|
||||
|
||||
//此法为归并排序自顶向下 空间复杂度O(logn)
|
||||
public ListNode sortList(ListNode head, ListNode tail) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
if (head.next == tail) {
|
||||
head.next = null;
|
||||
return head;
|
||||
}
|
||||
// 快慢指针,将链表分成两部分
|
||||
ListNode slow = head;
|
||||
ListNode fast = head;
|
||||
while (fast != tail) {
|
||||
slow = slow.next;
|
||||
fast = fast.next;
|
||||
if (fast != tail) {
|
||||
fast = fast.next;
|
||||
}
|
||||
}
|
||||
ListNode list1 = sortList(head, slow);
|
||||
ListNode list2 = sortList(slow, fast);
|
||||
return merge(list1, list2);
|
||||
}
|
||||
|
||||
public ListNode merge(ListNode node1, ListNode node2) {
|
||||
ListNode returnNode = new ListNode(0);
|
||||
ListNode temp = returnNode;
|
||||
while (node1 != null && node2 != null) {
|
||||
if (node1.val <= node2.val) {
|
||||
temp.next = node1;
|
||||
node1 = node1.next;
|
||||
} else {
|
||||
temp.next = node2;
|
||||
node2 = node2.next;
|
||||
}
|
||||
temp = temp.next;
|
||||
}
|
||||
|
||||
temp.next = node1 == null ? node2 : node1;
|
||||
|
||||
return returnNode.next;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
ListNode node1=new ListNode(0);
|
||||
ListNode node2=new ListNode(4,node1);
|
||||
ListNode node3=new ListNode(3,node2);
|
||||
ListNode node4=new ListNode(5,node3);
|
||||
ListNode node5=new ListNode(-1,node4);
|
||||
sortList(node5);
|
||||
}
|
||||
|
||||
}
|
|
@ -12,17 +12,17 @@ import java.util.Arrays;
|
|||
*/
|
||||
public class LeetCode20240825 {
|
||||
|
||||
public boolean canPartitionKSubsets(int[] nums, int k) {
|
||||
int sum = Arrays.stream(nums).sum();
|
||||
if(sum%k!=0){
|
||||
return false;
|
||||
}
|
||||
int average=sum/k;
|
||||
Arrays.sort(nums);
|
||||
int n = nums.length;
|
||||
if (nums[n - 1] > average) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// public boolean canPartitionKSubsets(int[] nums, int k) {
|
||||
// int sum = Arrays.stream(nums).sum();
|
||||
// if(sum%k!=0){
|
||||
// return false;
|
||||
// }
|
||||
// int average=sum/k;
|
||||
// Arrays.sort(nums);
|
||||
// int n = nums.length;
|
||||
// if (nums[n - 1] > average) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue