feat: 2025-10-24打卡
This commit is contained in:
59
160.intersection-of-two-linked-lists.java
Normal file
59
160.intersection-of-two-linked-lists.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* @lc app=leetcode id=160 lang=java
|
||||
*
|
||||
* [160] Intersection of Two Linked Lists
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
if(headA == null || headB == null) {
|
||||
return null;
|
||||
}
|
||||
int la = len(headA), lb = len(headB);
|
||||
ListNode cura = headA, curb = headB;
|
||||
if(la < lb) {
|
||||
// move curB
|
||||
for(int i=0; i<lb-la; i++) {
|
||||
curb = curb.next;
|
||||
}
|
||||
} else {
|
||||
// move curA
|
||||
for(int i = 0; i<la-lb; i++) {
|
||||
cura = cura.next;
|
||||
}
|
||||
}
|
||||
ListNode r = null;
|
||||
while(cura!=null) {
|
||||
if(cura == curb) {
|
||||
r = cura;
|
||||
break;
|
||||
}
|
||||
cura = cura.next;
|
||||
curb = curb.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private int len(ListNode head) {
|
||||
int l = 0;
|
||||
while(head != null) {
|
||||
l++;
|
||||
head = head.next;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
56
2.add-two-numbers.java
Normal file
56
2.add-two-numbers.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* @lc app=leetcode id=2 lang=java
|
||||
*
|
||||
* [2] Add Two Numbers
|
||||
*/
|
||||
|
||||
// @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) {
|
||||
boolean propagated = false;
|
||||
ListNode r = null, cur = null;
|
||||
while(l1!=null ||l2!=null) {
|
||||
int v1 = l1!=null?l1.val:0;
|
||||
int v2 = l2!=null?l2.val:0;
|
||||
int v = v1 + v2 + (propagated?1:0);
|
||||
propagated = v/10!=0;
|
||||
v = v %10;
|
||||
// add node to result
|
||||
ListNode t = new ListNode(v);
|
||||
t.next = null;
|
||||
if(r == null) {
|
||||
r = t;
|
||||
cur = t;
|
||||
} else {
|
||||
cur.next = t;
|
||||
cur = cur.next;
|
||||
}
|
||||
|
||||
if(l1!=null) {
|
||||
l1 = l1.next;
|
||||
}
|
||||
if(l2!=null) {
|
||||
l2 = l2.next;
|
||||
}
|
||||
}
|
||||
if(propagated) {
|
||||
ListNode t = new ListNode(1);
|
||||
t.next = null;
|
||||
cur.next = t;
|
||||
cur = cur.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
38
203.remove-linked-list-elements.java
Normal file
38
203.remove-linked-list-elements.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* @lc app=leetcode id=203 lang=java
|
||||
*
|
||||
* [203] Remove Linked List Elements
|
||||
*/
|
||||
|
||||
// @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 removeElements(ListNode head, int val) {
|
||||
if(head == null) {
|
||||
return null;
|
||||
}
|
||||
while(head!=null && head.val == val) {
|
||||
head = head.next;
|
||||
}
|
||||
ListNode cur = head;
|
||||
while(cur != null && cur.next != null) {
|
||||
if(cur.next.val == val) {
|
||||
cur.next = cur.next.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
31
206.reverse-linked-list.java
Normal file
31
206.reverse-linked-list.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* @lc app=leetcode id=206 lang=java
|
||||
*
|
||||
* [206] Reverse 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 reverseList(ListNode head) {
|
||||
ListNode prev = null, cur = head, t;
|
||||
while(cur != null) {
|
||||
t = cur.next;
|
||||
cur.next = prev;
|
||||
prev = cur;
|
||||
cur = t;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
54
234.palindrome-linked-list.java
Normal file
54
234.palindrome-linked-list.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* @lc app=leetcode id=234 lang=java
|
||||
*
|
||||
* [234] Palindrome 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 {
|
||||
/**
|
||||
* linked list求中点方案:快慢指针:
|
||||
* 当fast移动n次时,列表节点个数为1+2n或2n
|
||||
* 此时,slow移动n次时遍历节点个数为1+n
|
||||
* 即,节点总个数为奇数时,slow刚好位于中点
|
||||
* 当节点总个数为偶数时,slow位于n+1节点,即下半部分链表的开始节点
|
||||
* @param head
|
||||
* @return
|
||||
*/
|
||||
public boolean isPalindrome(ListNode head) {
|
||||
ListNode slow = head, fast = head, prev = null, t;
|
||||
while(fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
t = slow.next;
|
||||
slow.next = prev;
|
||||
prev = slow;
|
||||
slow = t;
|
||||
}
|
||||
// 此时,prev为上半部分开始节点,cur为下半部分开始节点
|
||||
// 如果fast不为空,节点总数为奇数,fast为空,节点总数为偶数
|
||||
if(fast != null) {
|
||||
slow = slow.next;
|
||||
}
|
||||
boolean f = true;
|
||||
while(slow != null) {
|
||||
if(slow.val != prev.val) {
|
||||
f = false;
|
||||
}
|
||||
slow = slow.next;
|
||||
prev = prev.next;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
38
766.toeplitz-matrix.java
Normal file
38
766.toeplitz-matrix.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* @lc app=leetcode id=766 lang=java
|
||||
*
|
||||
* [766] Toeplitz Matrix
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean isToeplitzMatrix(int[][] matrix) {
|
||||
for(int i=0; i<matrix.length; i++) {
|
||||
if(!sameElementsLTRP(matrix, i, 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for(int j = 0; j<matrix[0].length; j++) {
|
||||
if(!sameElementsLTRP(matrix, 0, j)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean sameElementsLTRP(int[][] matrix, int i, int j) {
|
||||
int e = matrix[i][j];
|
||||
boolean f = true;
|
||||
while(i < matrix.length && j < matrix[0].length) {
|
||||
if(matrix[i][j] != e) {
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
36
83.remove-duplicates-from-sorted-list.java
Normal file
36
83.remove-duplicates-from-sorted-list.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @lc app=leetcode id=83 lang=java
|
||||
*
|
||||
* [83] Remove Duplicates from Sorted 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 deleteDuplicates(ListNode head) {
|
||||
if(head == null || head.next == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode cur = head;
|
||||
while(cur!=null && cur.next != null) {
|
||||
if(cur.val == cur.next.val) {
|
||||
// remove cur.next
|
||||
cur.next = cur.next.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
26
908.smallest-range-i.java
Normal file
26
908.smallest-range-i.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=908 lang=java
|
||||
*
|
||||
* [908] Smallest Range I
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int smallestRangeI(int[] nums, int k) {
|
||||
if(nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int min = nums[0], max = nums[0];
|
||||
for(int i = 1; i < nums.length; i++) {
|
||||
if(nums[i] < min) {
|
||||
min = nums[i];
|
||||
}
|
||||
if(nums[i] > max) {
|
||||
max = nums[i];
|
||||
}
|
||||
}
|
||||
return max - min > 2 * k ? max - min - 2 * k : 0;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user