2024/8/24 LeetCode Hot100 LinkedList以及8.25的每日一题(尚未完成)

This commit is contained in:
Cool 2024-08-25 01:45:56 +08:00
parent c6d8c26bfd
commit e534a5f996
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.cool.hot100.linkedlist;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/24/23:22
* @Description:
*/
public class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}

View File

@ -0,0 +1,59 @@
package com.cool.hot100.linkedlist;
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/24/23:21
* DayNumber 1
* Hard 2
*/
public class Num138 {
Map<Node, Node> cacheMap = new HashMap<>();
public Node copyRandomList(Node head) {
//ans1
// head = copyNode(head);
// return head;
//ans2
if(head==null){
return null;
}
for (Node node=head;node!=null;node=node.next.next){
Node newNode = new Node(node.val);
newNode.next=node.next;
node.next=newNode;
}
for (Node node=head;node!=null;node=node.next.next){
Node newNode = node.next;
newNode.random= node.random==null?null:node.random.next;
}
Node newHead=head.next;
for(Node node=head;node!=null;node=node.next){
Node newNode=node.next;
node.next=node.next.next;
newNode.next=newNode.next==null?null:newNode.next.next;
}
return newHead;
}
private Node copyNode(Node node) {
if (node == null) {
return null;
}
if (cacheMap.containsKey(node)) {
node = cacheMap.get(node);
return node;
}
Node copyNode = new Node(node.val);
cacheMap.put(node, copyNode);
copyNode.next = copyNode(node.next);
copyNode.random = copyNode(node.random);
return copyNode;
}
}

View File

@ -0,0 +1,16 @@
package com.cool.one_question_per_day;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/25/1:43
* @Description:
*/
public class LeetCode20240825 {
public int findPermutationDifference(String s, String t) {
}
}