From 1d73fc1520fc36b4d6c627fa6ed79ec0435dd5b5 Mon Sep 17 00:00:00 2001 From: starsingchow Date: Thu, 21 Mar 2024 00:03:21 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\232\204\344\270\213\346\240\207.java" | 37 +++++++++++++++++++ "java/78.\345\255\220\351\233\206.java" | 33 +++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 "java/28.\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.java" create mode 100644 "java/78.\345\255\220\351\233\206.java" diff --git "a/java/28.\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.java" "b/java/28.\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.java" new file mode 100644 index 0000000..4c7a00e --- /dev/null +++ "b/java/28.\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.java" @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode.cn id=28 lang=java + * + * [28] 找出字符串中第一个匹配项的下标 + */ + +// @lc code=start +class Solution { + public int strStr(String haystack, String needle) { + if(haystack == null || needle == null || haystack.length() == 0 || needle.length() == 0) { + return -1; + } + + int hLen = haystack.length(); + int nLen = needle.length(); + int i = 0; + int j = 0; + int temp = i; + while(i < hLen && j < nLen) { + if(haystack.charAt(i) == needle.charAt(j)) { + i++; + j++; + if(j == nLen) { + return temp; + } + } else { + i = temp+1; + j = 0; + temp = i; + } + } + + return -1; + } +} +// @lc code=end + diff --git "a/java/78.\345\255\220\351\233\206.java" "b/java/78.\345\255\220\351\233\206.java" new file mode 100644 index 0000000..9111836 --- /dev/null +++ "b/java/78.\345\255\220\351\233\206.java" @@ -0,0 +1,33 @@ +/* + * @lc app=leetcode.cn id=78 lang=java + * + * [78] 子集 + */ + +// @lc code=start +class Solution { + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + + if(nums == null) { + return null; + } + + List list = new ArrayList(); + + backtrack(nums, 0, nums.length, list, result); + return result; + } + + private void backtrack(int[] nums, int index, int nLen,List list, List> result) { + result.add(new ArrayList(list)); + + for(int i = index; i < nLen; i++) { + list.add(nums[i]); + backtrack(nums, i+1, nLen, list, result); + list.remove(list.size()-1); + } + } +} +// @lc code=end + From cfa9a4f5d74f8dc60f31723513f7145ec1647d08 Mon Sep 17 00:00:00 2001 From: pikapika Date: Wed, 3 Apr 2024 15:22:49 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0leetcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\244\247\346\267\261\345\272\246.java" | 39 ++++++++++++++ ...\344\272\214\345\217\211\346\240\221.java" | 46 ++++++++++++++++ ...\350\267\257\345\276\204\345\222\214.java" | 54 +++++++++++++++++++ ...\345\205\261\347\245\226\345\205\210.java" | 47 ++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100644 "java/104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.java" create mode 100644 "java/110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.java" create mode 100644 "java/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214.java" create mode 100644 "java/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.java" diff --git "a/java/104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.java" "b/java/104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.java" new file mode 100644 index 0000000..6f8cd59 --- /dev/null +++ "b/java/104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.java" @@ -0,0 +1,39 @@ +/* + * @lc app=leetcode.cn id=104 lang=java + * + * [104] 二叉树的最大深度 + */ + +// @lc code=start +/** + * 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 int maxDepth(TreeNode root) { + if(root == null) { + return 0; + } + + int left = maxDepth(root.left); + int right = maxDepth(root.right); + + if(left > right) { + return left + 1; + } + return right + 1; + } +} +// @lc code=end + diff --git "a/java/110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.java" "b/java/110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.java" new file mode 100644 index 0000000..3740323 --- /dev/null +++ "b/java/110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.java" @@ -0,0 +1,46 @@ +/* + * @lc app=leetcode.cn id=110 lang=java + * + * [110] 平衡二叉树 + */ + +// @lc code=start +/** + * 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 boolean isBalanced(TreeNode root) { + if(maxDepth(root) == -1) return false; + return true; + } + + private int maxDepth(TreeNode node) { + if(node == null) return 0; + + int left = maxDepth(node.left); + int right = maxDepth(node.right); + + if(left == -1 || right == -1 || left-right > 1 || right-left > 1) { + return -1; + } + + if(left > right) { + return left+1; + } + return right+1; + } +} +// @lc code=end + diff --git "a/java/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214.java" "b/java/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214.java" new file mode 100644 index 0000000..d1ae559 --- /dev/null +++ "b/java/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214.java" @@ -0,0 +1,54 @@ +/* + * @lc app=leetcode.cn id=124 lang=java + * + * [124] 二叉树中的最大路径和 + */ + +// @lc code=start +/** + * 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 int maxPathSum(TreeNode root) { + int[] result = maxPathSumDepth(root); + return result[0]; + } + + + public int[] maxPathSumDepth(TreeNode root) { + if(root == null) { + // 第一位为两边最大值, 第二位为单边最大值 + return new int[]{Integer.MIN_VALUE, 0}; + } + + int[] left = maxPathSumDepth(root.left); + int[] right = maxPathSumDepth(root.right); + + int[] result = new int[2]; + + // 求单边最大值 + if(left[1] > right[1]) { + result[1] = Math.max(left[1]+root.val, 0); + } else { + result[1] = Math.max(right[1]+root.val, 0); + } + + // 求两边加根最大值 + result[0] = Math.max(left[1]+right[1]+root.val, Math.max(left[0], right[0])); + return result; + } +} +// @lc code=end + diff --git "a/java/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.java" "b/java/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.java" new file mode 100644 index 0000000..f546622 --- /dev/null +++ "b/java/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.java" @@ -0,0 +1,47 @@ +/* + * @lc app=leetcode.cn id=236 lang=java + * + * [236] 二叉树的最近公共祖先 + */ + +// @lc code=start +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if(root == null) { + return root; + } + + if(root.val == p.val || root.val == q.val) { + return root; + } + + // 二分法 + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + + if(left != null && right != null) { + return root; + } + + if(left != null) { + return left; + } + + if(right != null) { + return right; + } + + return null; + } +} +// @lc code=end + From e032141f8d8cfcb40859da40a0078cb86e4921e0 Mon Sep 17 00:00:00 2001 From: pikapika Date: Sun, 7 Apr 2024 11:31:55 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\272\217\351\223\276\350\241\250.java" | 46 +++++++++++++++ ...\351\232\224\351\223\276\350\241\250.java" | 44 +++++++++++++++ ...50\275\254\351\223\276\350\241\250II.java" | 56 +++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 "java/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.java" create mode 100644 "java/86.\345\210\206\351\232\224\351\223\276\350\241\250.java" create mode 100644 "java/92.\345\217\215\350\275\254\351\223\276\350\241\250II.java" diff --git "a/java/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.java" "b/java/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.java" new file mode 100644 index 0000000..be08dbc --- /dev/null +++ "b/java/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.java" @@ -0,0 +1,46 @@ +/* + * @lc app=leetcode.cn id=21 lang=java + * + * [21] 合并两个有序链表 + */ + +// @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 mergeTwoLists(ListNode list1, ListNode list2) { + ListNode dummy = new ListNode(); + ListNode curr = dummy; + while(list1 != null && list2 != null) { + if(list1.val < list2.val) { + curr.next = list1; + list1 = list1.next; + } else { + curr.next = list2; + list2 = list2.next; + } + + curr = curr.next; + } + + if(list1 != null) { + curr.next = list1; + } + + if(list2 != null) { + curr.next = list2; + } + + return dummy.next; + } +} +// @lc code=end + diff --git "a/java/86.\345\210\206\351\232\224\351\223\276\350\241\250.java" "b/java/86.\345\210\206\351\232\224\351\223\276\350\241\250.java" new file mode 100644 index 0000000..b685a88 --- /dev/null +++ "b/java/86.\345\210\206\351\232\224\351\223\276\350\241\250.java" @@ -0,0 +1,44 @@ +/* + * @lc app=leetcode.cn id=86 lang=java + * + * [86] 分隔链表 + */ + +// @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) { + if(head == null) return null; + ListNode node1 = new ListNode(); + ListNode curr1 = node1; + ListNode node2 = new ListNode(); + ListNode curr2 = node2; + + while(head != null) { + if(head.val < x) { + curr1.next = head; + curr1 = curr1.next; + } else { + curr2.next = head; + curr2 = curr2.next; + } + + head = head.next; + } + + curr1.next = node2.next; + curr2.next = null; + return node1.next; + } +} +// @lc code=end + diff --git "a/java/92.\345\217\215\350\275\254\351\223\276\350\241\250II.java" "b/java/92.\345\217\215\350\275\254\351\223\276\350\241\250II.java" new file mode 100644 index 0000000..a21dbc4 --- /dev/null +++ "b/java/92.\345\217\215\350\275\254\351\223\276\350\241\250II.java" @@ -0,0 +1,56 @@ +/* + * @lc app=leetcode.cn id=92 lang=java + * + * [92] 反转链表 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 reverseBetween(ListNode head, int left, int right) { + if(head == null) { + return head; + } + + ListNode dummy = new ListNode(0); + dummy.next = head; + + ListNode pre = null; + int i = 1; + while(i < left) { + pre = head; + head = head.next; + i++; + } + + int j = i; + ListNode next = null; + ListNode mid = head; + while(head != null && j <= right) { + ListNode temp = head.next; + head.next = next; + next = head; + head = temp; + j++; + } + + if(pre != null) { + pre.next = next; + } else { + dummy.next = next; + } + mid.next = head; + return dummy.next; + } +} +// @lc code=end + From c72ba039b962237ea9ab1f25f34a0f93ce106781 Mon Sep 17 00:00:00 2001 From: pikapika Date: Sun, 7 Apr 2024 14:21:00 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\272\217\351\223\276\350\241\250.java" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "java/148.\346\216\222\345\272\217\351\223\276\350\241\250.java" diff --git "a/java/148.\346\216\222\345\272\217\351\223\276\350\241\250.java" "b/java/148.\346\216\222\345\272\217\351\223\276\350\241\250.java" new file mode 100644 index 0000000..55d8d26 --- /dev/null +++ "b/java/148.\346\216\222\345\272\217\351\223\276\350\241\250.java" @@ -0,0 +1,78 @@ +/* + * @lc app=leetcode.cn id=148 lang=java + * + * [148] 排序链表 + */ + +// @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) { + return mergeSort(head); + } + + public ListNode findMiddle(ListNode node) { + ListNode slow = node; + ListNode fast = node.next; + + while(fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + + return slow; + } + + public ListNode mergeTwoLists(ListNode node1, ListNode node2) { + ListNode dummy = new ListNode(); + ListNode head = dummy; + + while(node1 != null && node2 != null) { + if(node1.val < node2.val) { + head.next = node1; + node1 = node1.next; + } else { + head.next = node2; + node2 = node2.next; + } + + head = head.next; + } + + if(node1 != null) { + head.next = node1; + } + + if(node2 != null) { + head.next = node2; + } + + return dummy.next; + } + + public ListNode mergeSort(ListNode node) { + if(node == null || node.next == null) { + return node; + } + + ListNode middleNode = findMiddle(node); + ListNode middleNodeNext = middleNode.next; + middleNode.next = null; + + ListNode node1 = mergeSort(node); + ListNode node2 = mergeSort(middleNodeNext); + + return mergeTwoLists(node1, node2); + } +} +// @lc code=end + From 9f81c743866d0a54637b57fea15937f772409f52 Mon Sep 17 00:00:00 2001 From: pikapika Date: Mon, 8 Apr 2024 14:09:02 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\275\242\351\223\276\350\241\250.java" | 38 +++++++++++ ...45\275\242\351\223\276\350\241\250II.java" | 44 +++++++++++++ ...\346\226\207\351\223\276\350\241\250.java" | 63 +++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 "java/141.\347\216\257\345\275\242\351\223\276\350\241\250.java" create mode 100644 "java/142.\347\216\257\345\275\242\351\223\276\350\241\250II.java" create mode 100644 "java/234.\345\233\236\346\226\207\351\223\276\350\241\250.java" diff --git "a/java/141.\347\216\257\345\275\242\351\223\276\350\241\250.java" "b/java/141.\347\216\257\345\275\242\351\223\276\350\241\250.java" new file mode 100644 index 0000000..f89fc0a --- /dev/null +++ "b/java/141.\347\216\257\345\275\242\351\223\276\350\241\250.java" @@ -0,0 +1,38 @@ +/* + * @lc app=leetcode.cn id=141 lang=java + * + * [141] 环形链表 + */ + +// @lc code=start +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + public boolean hasCycle(ListNode head) { + if(head == null) return false; + ListNode slow = head; + ListNode fast = head.next; + + while(fast != null && fast.next != null) { + if(slow == fast) { + return true; + } + + slow = slow.next; + fast = fast.next.next; + } + + return false; + } +} +// @lc code=end + diff --git "a/java/142.\347\216\257\345\275\242\351\223\276\350\241\250II.java" "b/java/142.\347\216\257\345\275\242\351\223\276\350\241\250II.java" new file mode 100644 index 0000000..f45bfc7 --- /dev/null +++ "b/java/142.\347\216\257\345\275\242\351\223\276\350\241\250II.java" @@ -0,0 +1,44 @@ +/* + * @lc app=leetcode.cn id=142 lang=java + * + * [142] 环形链表 II + */ + +// @lc code=start +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + public ListNode detectCycle(ListNode head) { + if(head == null) return null; + + ListNode slow = head; + ListNode fast = head; + + while(fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + if(slow == fast) { + fast = head; + while(fast != slow) { + fast = fast.next; + slow = slow.next; + } + + return slow; + } + } + + return null; + } +} +// @lc code=end + diff --git "a/java/234.\345\233\236\346\226\207\351\223\276\350\241\250.java" "b/java/234.\345\233\236\346\226\207\351\223\276\350\241\250.java" new file mode 100644 index 0000000..c3aebc6 --- /dev/null +++ "b/java/234.\345\233\236\346\226\207\351\223\276\350\241\250.java" @@ -0,0 +1,63 @@ +/* + * @lc app=leetcode.cn id=234 lang=java + * + * [234] 回文链表 + */ + +// @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 boolean isPalindrome(ListNode head) { + if(head == null) return true; + + ListNode slow = head; + ListNode fast = head.next; + + while(fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + ListNode tail = reverse(slow.next); + slow.next = null; + + while(head != null && tail != null) { + if(head.val != tail.val) { + return false; + } + + head = head.next; + tail = tail.next; + } + + return true; + + } + + private ListNode reverse(ListNode node) { + if(node == null) return null; + + ListNode dummy = new ListNode(); + while(node != null) { + ListNode tmp = node.next; + node.next = dummy.next; + dummy.next = node; + node = tmp; + } + + return dummy.next; + } + + +} +// @lc code=end +