feat: 2025-10-27打卡

This commit is contained in:
wu xiangkai
2025-10-27 16:22:33 +08:00
parent 6dc597fa75
commit d910c716e6
10 changed files with 501 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
/*
* @lc app=leetcode id=109 lang=java
*
* [109] Convert Sorted List to Binary Search Tree
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedListToBST(ListNode head) {
return buildTree(head);
}
private TreeNode buildTree(ListNode head) {
if(head == null || head.next == null) {
return head == null ? null : new TreeNode(head.val, null, null);
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode slow = dummy, fast = dummy, prev = dummy;
while(fast != null && fast.next != null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = null;
TreeNode tree = new TreeNode(slow.val, null, null);
tree.left = buildTree(dummy.next);
tree.right = buildTree(slow.next);
return tree;
}
}
// @lc code=end

View File

@@ -0,0 +1,46 @@
/*
* @lc app=leetcode id=138 lang=java
*
* [138] Copy List with Random Pointer
*/
// @lc code=start
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
if(head == null) {
return null;
}
Map<Node, Node> ref = new HashMap<>();
Node cur = head;
while(cur != null) {
Node cp = new Node(cur.val);
ref.put(cur, cp);
cur = cur.next;
}
cur = head;
while(cur != null) {
Node nd = ref.get(cur);
nd.next = ref.get(cur.next);
nd.random = ref.get(cur.random);
cur = cur.next;
}
return ref.get(head);
}
}
// @lc code=end

63
148.sort-list.java Normal file
View File

@@ -0,0 +1,63 @@
/*
* @lc app=leetcode id=148 lang=java
*
* [148] Sort List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode slow = head, fast = head, prev = null;
while(fast != null && fast.next != null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = null;
ListNode l1 = sortList(head);
ListNode l2 = sortList(slow);
return merge(l1, l2);
}
private ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), tail = dummy, tmp;
dummy.next = null;
while(l1 != null || l2 != null) {
if(l1 == null || l2 == null) {
if(l1 != null) {
tail.next = l1;
} else {
tail.next = l2;
}
break;
} else {
if(l1.val <= l2.val) {
tmp = l1;
l1 = l1.next;
} else {
tmp = l2;
l2 = l2.next;
}
}
tail.next = tmp;
tail = tail.next;
tail.next = null;
}
return dummy.next;
}
}
// @lc code=end

View File

@@ -0,0 +1,33 @@
/*
* @lc app=leetcode id=237 lang=java
*
* [237] Delete Node in a Linked List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
if(node == null || node.next == null) {
return;
}
ListNode cur = node, prev = null;
while(cur!=null && cur.next!=null) {
cur.val = cur.next.val;
prev = cur;
cur = cur.next;
}
if(prev != null) {
prev.next = null;
}
}
}
// @lc code=end

View File

@@ -0,0 +1,44 @@
/*
* @lc app=leetcode id=328 lang=java
*
* [328] Odd Even Linked List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode oddDummy = new ListNode(0), oddTail = oddDummy, evenDummy = new ListNode(0), evenTail = evenDummy, cur = head;
int i = 1;
while(cur != null) {
if(i % 2 == 1) {
oddTail.next = cur;
cur = cur.next;
oddTail = oddTail.next;
oddTail.next = null;
} else {
evenTail.next = cur;
cur = cur.next;
evenTail = evenTail.next;
evenTail.next = null;
}
i++;
}
oddTail.next = evenDummy.next;
return oddDummy.next;
}
}
// @lc code=end

View File

@@ -0,0 +1,70 @@
/*
* @lc app=leetcode id=445 lang=java
*
* [445] Add Two Numbers II
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverse(l1);
l2 = reverse(l2);
return reverse(addByLittleEnd(l1, l2));
}
private ListNode addByLittleEnd(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), tail = dummy;
dummy.next = null;
boolean flow = false;
while(l1 != null || l2 != null) {
int v1 = l1==null?0:l1.val;
int v2 = l2==null?0:l2.val;
int v = v1 + v2 + (flow?1:0);
flow = v>=10;
ListNode tmp = new ListNode(v%10, null);
tail.next = tmp;
tail = tail.next;
tail.next = null;
if(l1 != null) {
l1 = l1.next;
}
if(l2 != null) {
l2 = l2.next;
}
}
if(flow) {
ListNode tmp = new ListNode(1, null);
tail.next = tmp;
tail = tail.next;
tail.next = null;
}
return dummy.next;
}
private ListNode reverse(ListNode l) {
if(l == null) {
return l;
}
ListNode dummy = new ListNode(0), tmp;
dummy.next = null;
while(l != null) {
tmp = l.next;
l.next = dummy.next;
dummy.next = l;
l = tmp;
}
return dummy.next;
}
}
// @lc code=end

45
61.rotate-list.java Normal file
View File

@@ -0,0 +1,45 @@
/*
* @lc app=leetcode id=61 lang=java
*
* [61] Rotate List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null) {
return head;
}
int len = 0;
ListNode cur = head;
while(cur != null) {
len++;
if(cur.next == null) {
cur.next = head;
break;
}
cur = cur.next;
}
k = k % len;
ListNode prev = cur;
cur = head;
for(int i = 0; i < len - k; i++) {
cur = cur.next;
prev = prev.next;
}
prev.next = null;
return cur;
}
}
// @lc code=end

View File

@@ -0,0 +1,42 @@
/*
* @lc app=leetcode id=82 lang=java
*
* [82] Remove Duplicates from Sorted List II
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
dummy.next = null;
ListNode cur = head;
int v;
while(cur != null) {
if(cur.next == null || cur.val != cur.next.val) {
tail.next = cur;
cur = cur.next;
tail = tail.next;
tail.next = null;
} else {
v = cur.val;
while(cur != null && cur.val == v) {
cur = cur.next;
}
}
}
return dummy.next;
}
}
// @lc code=end

57
838.push-dominoes.java Normal file
View File

@@ -0,0 +1,57 @@
/*
* @lc app=leetcode id=838 lang=java
*
* [838] Push Dominoes
*/
// @lc code=start
class Solution {
public String pushDominoes(String dominoes) {
final int N = dominoes.length();
int[] states = new int[dominoes.length()];
for(int i = 0; i<dominoes.length(); i++) {
if(dominoes.charAt(i) == 'L') {
states[i] -= N;
int j = i-1;
int fluence = N;
while(j>=0) {
--fluence;
states[j] -= fluence;
if(dominoes.charAt(j) !='.') {
break;
}
j--;
}
} else if(dominoes.charAt(i) == 'R') {
states[i] += N;
int j = i+1;
int fluence = N;
while(j < dominoes.length()) {
--fluence;
states[j] += fluence;
if(dominoes.charAt(j) != '.') {
break;
}
j++;
}
} else {
continue;
}
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i<states.length; i++) {
if(states[i] < 0) {
sb.append('L');
} else if(states[i] > 0) {
sb.append('R');
} else {
sb.append('.');
}
}
return sb.toString();
}
}
// @lc code=end

43
86.partition-list.java Normal file
View File

@@ -0,0 +1,43 @@
/*
* @lc app=leetcode id=86 lang=java
*
* [86] Partition List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode tf = new ListNode(0),
tb = new ListNode(0);
tf.next = null;
tb.next = null;
ListNode cur = head, tailf = tf, tailb = tb;
while(cur != null) {
if(cur.val < x) {
tailf.next = cur;
cur = cur.next;
tailf = tailf.next;
tailf.next = null;
} else {
tailb.next = cur;
cur = cur.next;
tailb = tailb.next;
tailb.next = null;
}
}
tailf.next = tb.next;
return tf.next;
}
}
// @lc code=end