docs(LeetCode): LeetCodeHot100

LeetCode刷题
This commit is contained in:
Zerroi 2024-04-14 16:50:22 +08:00
parent cca9883795
commit fcc9fd84a9
12 changed files with 361 additions and 37 deletions

View File

@ -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;
}
}

View File

@ -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; // 返回真正的头节点
}
}

View File

@ -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;

View File

@ -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; // 返回真正的头节点
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<String> 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;
}
}

View File

@ -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<Integer> 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;
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}