From fcc9fd84a975f0097c6aea31ac28a0341b7a23f3 Mon Sep 17 00:00:00 2001 From: Zerroi Date: Sun, 14 Apr 2024 16:50:22 +0800 Subject: [PATCH] docs(LeetCode): LeetCodeHot100 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LeetCode刷题 --- com/zerroi/leetcode/Four14/MaxProfit.java | 19 +++++++ com/zerroi/leetcode/Three25/ListNode.java | 33 +++++++++++ .../leetcode/Three25/RemoveNthFromEnd.java | 40 ++++++------- com/zerroi/leetcode/Three26/ListNode.java | 34 +++++++++++ com/zerroi/leetcode/Three26/Partition.java | 32 +++++++++++ com/zerroi/leetcode/Three26/RotateRight.java | 49 ++++++++++++++++ .../leetcode/Three27/CountSubstrings.java | 44 +++++++++++++++ .../leetcode/Three27/DailyTemperatures.java | 56 +++++++++++++++++++ com/zerroi/leetcode/sort/BubbleSort.java | 8 ++- com/zerroi/leetcode/sort/InsertionSort.java | 19 +++---- com/zerroi/leetcode/sort/QuickSort.java | 49 ++++++++++++++++ com/zerroi/leetcode/sort/SelectionSort.java | 15 +++-- 12 files changed, 361 insertions(+), 37 deletions(-) create mode 100644 com/zerroi/leetcode/Four14/MaxProfit.java create mode 100644 com/zerroi/leetcode/Three25/ListNode.java create mode 100644 com/zerroi/leetcode/Three26/ListNode.java create mode 100644 com/zerroi/leetcode/Three26/Partition.java create mode 100644 com/zerroi/leetcode/Three26/RotateRight.java create mode 100644 com/zerroi/leetcode/Three27/CountSubstrings.java create mode 100644 com/zerroi/leetcode/Three27/DailyTemperatures.java create mode 100644 com/zerroi/leetcode/sort/QuickSort.java diff --git a/com/zerroi/leetcode/Four14/MaxProfit.java b/com/zerroi/leetcode/Four14/MaxProfit.java new file mode 100644 index 0000000..b4ff982 --- /dev/null +++ b/com/zerroi/leetcode/Four14/MaxProfit.java @@ -0,0 +1,19 @@ +package com.zerroi.leetcode.Four14; + +public class MaxProfit { + public static void main(String[] args) { + + } +} + +class SolutionSecond { + public int maxProfit(int[] prices) { + int profit = Integer.MIN_VALUE; + int cost = Integer.MAX_VALUE; + for (int price : prices) { + cost = Math.min(cost, price); + profit = Math.max(profit, price - cost); + } + return profit; + } +} diff --git a/com/zerroi/leetcode/Three25/ListNode.java b/com/zerroi/leetcode/Three25/ListNode.java new file mode 100644 index 0000000..9b3e8be --- /dev/null +++ b/com/zerroi/leetcode/Three25/ListNode.java @@ -0,0 +1,33 @@ +package com.zerroi.leetcode.Three25; + +class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } + public static ListNode constructLinkedList(int[] nums) { + if (nums == null || nums.length == 0) { + return null; + } + + ListNode dummy = new ListNode(); // 创建一个虚拟头节点 + ListNode current = dummy; + + for (int num : nums) { + current.next = new ListNode(num); + current = current.next; + } + + return dummy.next; // 返回真正的头节点 + } +} diff --git a/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java b/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java index 93bdcad..a7628be 100644 --- a/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java +++ b/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java @@ -3,30 +3,32 @@ package com.zerroi.leetcode.Three25; public class RemoveNthFromEnd { public static void main(String[] args) { // ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))); - ListNode head = new ListNode(1, new ListNode(2)); + ListNode head = ListNode.constructLinkedList(new int[]{1,2,3,4,5}); SolutionFirst solutionFirst = new SolutionFirst(); - solutionFirst.removeNthFromEnd(head, 2); - } -} - -class ListNode { - int val; - ListNode next; - - ListNode() { - } - - ListNode(int val) { - this.val = val; - } - - ListNode(int val, ListNode next) { - this.val = val; - this.next = next; + solutionFirst.removeNthFromEnd2(head, 2); } } class SolutionFirst { + + public ListNode removeNthFromEnd2(ListNode head, int n) { + ListNode dummy = new ListNode(-1, head); + ListNode slow = dummy; + ListNode fast = dummy; + for (int i = 0; i < n; i++) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + if (slow.next != null) { + slow.next = slow.next.next; + } + return dummy.next; + } + + public ListNode removeNthFromEnd(ListNode head, int n) { if(head == null) return null; ListNode cur = head; diff --git a/com/zerroi/leetcode/Three26/ListNode.java b/com/zerroi/leetcode/Three26/ListNode.java new file mode 100644 index 0000000..52befff --- /dev/null +++ b/com/zerroi/leetcode/Three26/ListNode.java @@ -0,0 +1,34 @@ +package com.zerroi.leetcode.Three26; + +class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } + + public static ListNode constructLinkedList(int[] nums) { + if (nums == null || nums.length == 0) { + return null; + } + + ListNode dummy = new ListNode(); // 创建一个虚拟头节点 + ListNode current = dummy; + + for (int num : nums) { + current.next = new ListNode(num); + current = current.next; + } + + return dummy.next; // 返回真正的头节点 + } +} diff --git a/com/zerroi/leetcode/Three26/Partition.java b/com/zerroi/leetcode/Three26/Partition.java new file mode 100644 index 0000000..fcd20cf --- /dev/null +++ b/com/zerroi/leetcode/Three26/Partition.java @@ -0,0 +1,32 @@ +package com.zerroi.leetcode.Three26; + +public class Partition { + public static void main(String[] args) { + ListNode head = ListNode.constructLinkedList(new int[]{1, 4, 3, 2, 5, 2}); + SolutionSecond solutionSecond = new SolutionSecond(); + ListNode res = solutionSecond.partition(head, 3); + } +} + +class SolutionSecond { + public ListNode partition(ListNode head, int x) { + if (head == null) return null; + ListNode dummy1 = new ListNode(-1); + ListNode cur1 = dummy1; + ListNode dummy2 = new ListNode(-1); + ListNode cur2 = dummy2; + while (head != null) { + if (head.val < x) { + cur1.next = new ListNode(head.val); + cur1 = cur1.next; + } else { + cur2.next = new ListNode(head.val); + cur2 = cur2.next; + } + head = head.next; + } + cur1.next = dummy2.next; + + return dummy1.next; + } +} diff --git a/com/zerroi/leetcode/Three26/RotateRight.java b/com/zerroi/leetcode/Three26/RotateRight.java new file mode 100644 index 0000000..ad47ee2 --- /dev/null +++ b/com/zerroi/leetcode/Three26/RotateRight.java @@ -0,0 +1,49 @@ +package com.zerroi.leetcode.Three26; + +import org.w3c.dom.ls.LSException; + + +public class RotateRight { + public static void main(String[] args) { + ListNode head = ListNode.constructLinkedList(new int[]{1, 2, 3, 4, 5}); + SolutionFirst solutionFirst = new SolutionFirst(); + ListNode node = solutionFirst.rotateRight(head, 2); + } + + public static void print(ListNode head) { + while (head != null) { + System.out.println("head = " + head.val); + head = head.next; + } + } +} + +class SolutionFirst { + public ListNode rotateRight(ListNode head, int k) { +// 向右移动k个 可以等价于 将链表后k个节点放到链表头部 + int length = length(head); + k = k % length; + if (k == 0) return head; + ListNode cur = head; + for (int i = 1; i < length - k; i++) { + cur = cur.next; + } + ListNode temp = cur.next; + cur.next = null; + ListNode cur1 = temp; + while (cur1.next != null) { + cur1 = cur1.next; + } + cur1.next = head; + return temp; + } + + public int length(ListNode head) { + int count = 0; + while (head != null) { + count++; + head = head.next; + } + return count; + } +} diff --git a/com/zerroi/leetcode/Three27/CountSubstrings.java b/com/zerroi/leetcode/Three27/CountSubstrings.java new file mode 100644 index 0000000..c51d55c --- /dev/null +++ b/com/zerroi/leetcode/Three27/CountSubstrings.java @@ -0,0 +1,44 @@ +package com.zerroi.leetcode.Three27; + +import java.util.ArrayList; +import java.util.List; + +public class CountSubstrings { + public static void main(String[] args) { + Solution solution = new Solution(); + String s = "abc"; + int res = solution.countSubstrings(s); + System.out.println("res = " + res); + } +} + +class Solution { + private List path = new ArrayList<>(); + public int countSubstrings(String s) { + backtrack(s, 0); + return path.size(); + } + private void backtrack(String s, int start) { + if (start >= s.length()) { + return; + } + for (int i = start; i < s.length(); i++) { + if (isPalindrome(s, start, i)) { + String str = s.substring(start, i + 1); + path.add(str); + } else { + continue; + } + backtrack(s, i + 1); + } + } + + private boolean isPalindrome(String s, int startIndex, int end) { + for (int i = startIndex, j = end; i < j; i++, j--) { + if (s.charAt(i) != s.charAt(j)) { + return false; + } + } + return true; + } +} diff --git a/com/zerroi/leetcode/Three27/DailyTemperatures.java b/com/zerroi/leetcode/Three27/DailyTemperatures.java new file mode 100644 index 0000000..bf63b5a --- /dev/null +++ b/com/zerroi/leetcode/Three27/DailyTemperatures.java @@ -0,0 +1,56 @@ +package com.zerroi.leetcode.Three27; + +import java.util.Stack; + +public class DailyTemperatures { + public static void main(String[] args) { + SolutionFirst solutionFirst = new SolutionFirst(); + int[] res = solutionFirst.dailyTemperatures(new int[]{55, 38, 53, 81, 61, 93, 97, 32, 43, 78}); + } +} + +class SolutionFirst { + public int[] dailyTemperatures(int[] temperatures) { + int[] next = new int[temperatures.length]; + for (int i = 0; i < temperatures.length - 1; i++) { + int top = temperatures[i]; + int index = i + 1; + if (temperatures[i + 1] > top) { + next[i]++; + } else { + boolean flag = false; + while (index < temperatures.length) { + if (temperatures[index] > top) { + flag = true; + break; + } + index++; + if (index > 200) { + break; + } + } + if (flag) { + next[i] = index - i; + } else { + next[i] = 0; + } + } + } + return next; + } + + public int[] dailyTemperatures2(int[] temperatures) { + int[] next = new int[temperatures.length]; + Stack stack = new Stack<>(); + + for (int i = 0; i < temperatures.length; i++) { + while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { + int prevIndex = stack.pop(); + next[prevIndex] = i - prevIndex; + } + stack.push(i); + } + + return next; + } +} diff --git a/com/zerroi/leetcode/sort/BubbleSort.java b/com/zerroi/leetcode/sort/BubbleSort.java index 08a930b..51ef976 100644 --- a/com/zerroi/leetcode/sort/BubbleSort.java +++ b/com/zerroi/leetcode/sort/BubbleSort.java @@ -14,14 +14,18 @@ public class BubbleSort { class Bubble { public int[] bubble(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { + boolean flag = false; for (int j = 0; j < nums.length - i - 1; j++) { - int temp; if (nums[j] > nums[j + 1]) { - temp = nums[j]; + int temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; + flag = true; } } + if (flag) { + break; + } } return nums; } diff --git a/com/zerroi/leetcode/sort/InsertionSort.java b/com/zerroi/leetcode/sort/InsertionSort.java index 1811f77..f6ff222 100644 --- a/com/zerroi/leetcode/sort/InsertionSort.java +++ b/com/zerroi/leetcode/sort/InsertionSort.java @@ -14,17 +14,16 @@ public class InsertionSort { class Insertion { public int[] insertionSort(int[] nums) { for (int i = 1; i < nums.length; i++) { - int temp = nums[i]; - int j = i; - while (j > 0 && temp < nums[j - 1]) { - nums[j] = nums[j - 1]; - j--; - } - if (j != i) { - nums[j] = temp; - } + int temp = nums[i]; + int j = i; + while (j > 0 && temp < nums[j - 1]) { + nums[j] = nums[j - 1]; + j--; + } + if (j != i) { + nums[j] = temp; + } } - return nums; } } diff --git a/com/zerroi/leetcode/sort/QuickSort.java b/com/zerroi/leetcode/sort/QuickSort.java new file mode 100644 index 0000000..8844b01 --- /dev/null +++ b/com/zerroi/leetcode/sort/QuickSort.java @@ -0,0 +1,49 @@ +package com.zerroi.leetcode.sort; + + +public class QuickSort { + public static void main(String[] args) { + QuickSortImpl quickSort = new QuickSortImpl(); + int[] arr = {5, 1, 2, 6, 78, 1, 8, 45, 1, 645, 12}; + int[] res = quickSort.quickSort(arr, 0, arr.length - 1); + for (int re : res) { + System.out.println(re); + } + } +} +class QuickSortImpl { + + public int[] quickSort(int[] arr, int left, int right) { + if (left < right) { + int partition = partition(arr, left, right); + quickSort(arr, left, partition - 1); + quickSort(arr, partition + 1, right); + } + return arr; + } + + /* 元素交换 */ + void swap(int[] nums, int i, int j) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } + + /* 哨兵划分 */ + int partition(int[] nums, int left, int right) { + // 以 nums[left] 为基准数 + int i = left; + int j = right; + while (i < j) { + while (i < j && nums[left] >= nums[j]) { + j--; + } + while (i < j && nums[left] <= nums[i]) { + i++; + } + swap(nums, i, j); + } + swap(nums, i, left); + return i; + } +} diff --git a/com/zerroi/leetcode/sort/SelectionSort.java b/com/zerroi/leetcode/sort/SelectionSort.java index 0af71e5..66ba6f4 100644 --- a/com/zerroi/leetcode/sort/SelectionSort.java +++ b/com/zerroi/leetcode/sort/SelectionSort.java @@ -20,15 +20,18 @@ public class SelectionSort { class Selection { public int[] selection(int[] nums) { - for (int i = 0; i < nums.length; i++) { + for (int i = 0; i < nums.length - 1; i++) { + int min = i; for (int j = i + 1; j < nums.length; j++) { - int temp = 0; - if (nums[i] > nums[j]) { - temp = nums[i]; - nums[i] = nums[j]; - nums[j] = temp; + if (nums[j] < nums[min]) { + min = j; } } + if (min != i) { + int temp = nums[i]; + nums[i] = nums[min]; + nums[min] = temp; + } } return nums; }