From cca988379584c1b4ea172c49e86717dd5c544cab Mon Sep 17 00:00:00 2001 From: Zerroi Date: Mon, 25 Mar 2024 20:14:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E6=B2=B9=E5=86=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/Three21/IsValidBracket.java | 23 +++++ .../leetcode/Three21/MinStackClass.java | 10 ++ com/zerroi/leetcode/Three21/SimplifyPath.java | 41 ++++++++ .../leetcode/Three22/BasicCalculator.java | 98 +++++++++++++++++++ com/zerroi/leetcode/Three22/EvalRPN.java | 49 ++++++++++ com/zerroi/leetcode/Three22/HasCycle.java | 37 +++++++ .../leetcode/Three23/AddTwoNumbers.java | 57 +++++++++++ .../leetcode/Three23/MergeTwoLists.java | 36 +++++++ com/zerroi/leetcode/Three23/ReverseList.java | 22 +++++ .../leetcode/Three24/ReverseBetween.java | 52 ++++++++++ .../leetcode/Three25/DeleteDuplicates.java | 56 +++++++++++ .../leetcode/Three25/RemoveNthFromEnd.java | 55 +++++++++++ com/zerroi/leetcode/ThreeFive/Rotate.java | 8 +- 13 files changed, 538 insertions(+), 6 deletions(-) create mode 100644 com/zerroi/leetcode/Three21/IsValidBracket.java create mode 100644 com/zerroi/leetcode/Three21/MinStackClass.java create mode 100644 com/zerroi/leetcode/Three21/SimplifyPath.java create mode 100644 com/zerroi/leetcode/Three22/BasicCalculator.java create mode 100644 com/zerroi/leetcode/Three22/EvalRPN.java create mode 100644 com/zerroi/leetcode/Three22/HasCycle.java create mode 100644 com/zerroi/leetcode/Three23/AddTwoNumbers.java create mode 100644 com/zerroi/leetcode/Three23/MergeTwoLists.java create mode 100644 com/zerroi/leetcode/Three23/ReverseList.java create mode 100644 com/zerroi/leetcode/Three24/ReverseBetween.java create mode 100644 com/zerroi/leetcode/Three25/DeleteDuplicates.java create mode 100644 com/zerroi/leetcode/Three25/RemoveNthFromEnd.java diff --git a/com/zerroi/leetcode/Three21/IsValidBracket.java b/com/zerroi/leetcode/Three21/IsValidBracket.java new file mode 100644 index 0000000..81a34d3 --- /dev/null +++ b/com/zerroi/leetcode/Three21/IsValidBracket.java @@ -0,0 +1,23 @@ +package com.zerroi.leetcode.Three21; + +import java.util.Stack; + +public class IsValidBracket { + public static void main(String[] args) { + + } +} + +class SolutionFirst { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + + for (char c : s.toCharArray()) { + if (c == '(') stack.push(')'); + else if (c == '[') stack.push(']'); + else if (c == '{') stack.push('}'); + else if (stack.isEmpty() || stack.pop() != c) return false; + } + return stack.isEmpty(); + } +} diff --git a/com/zerroi/leetcode/Three21/MinStackClass.java b/com/zerroi/leetcode/Three21/MinStackClass.java new file mode 100644 index 0000000..00029ad --- /dev/null +++ b/com/zerroi/leetcode/Three21/MinStackClass.java @@ -0,0 +1,10 @@ +package com.zerroi.leetcode.Three21; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class MinStackClass { + +} + diff --git a/com/zerroi/leetcode/Three21/SimplifyPath.java b/com/zerroi/leetcode/Three21/SimplifyPath.java new file mode 100644 index 0000000..ff8ebac --- /dev/null +++ b/com/zerroi/leetcode/Three21/SimplifyPath.java @@ -0,0 +1,41 @@ +package com.zerroi.leetcode.Three21; + + +import java.util.Queue; +import java.util.Stack; + +public class SimplifyPath { + public static void main(String[] args) { + SolutionSecond solutionSecond = new SolutionSecond(); +// String s = solutionSecond.simplifyPath("/a//b////c/d//././/.."); + String s = solutionSecond.simplifyPath("/../"); + System.out.println("s = " + s); + + } +} + +class SolutionSecond { + public String simplifyPath(String path) { + String[] strs = path.split("/"); + Stack stack = new Stack<>(); + + for (String str : strs) { + if ("..".equals(str)) { + if (!stack.isEmpty()) stack.pop(); + } else if (!str.isEmpty() && !str.equals(".")) { + stack.push(str); + } + } + StringBuffer res = new StringBuffer(); + if (stack.isEmpty()) { + res.append("/"); + } else { + while (!stack.isEmpty()) { + res.append("/"); + res.append(stack.removeFirst()); + } + } + + return res.toString(); + } +} diff --git a/com/zerroi/leetcode/Three22/BasicCalculator.java b/com/zerroi/leetcode/Three22/BasicCalculator.java new file mode 100644 index 0000000..fbf0ab3 --- /dev/null +++ b/com/zerroi/leetcode/Three22/BasicCalculator.java @@ -0,0 +1,98 @@ +package com.zerroi.leetcode.Three22; + +import java.util.*; + +public class BasicCalculator { + public static void main(String[] args) { + Solution solution = new Solution(); + int res = solution.calculate("(1+(4+5+2)-3)+(6+8)"); + System.out.println("res = " + res); + } +} + +class Solution { + private Map map = new HashMap<>(); + private Deque opQue = new LinkedList<>(); + private Deque numQue = new LinkedList<>(); + + private void compute() { + int b = numQue.pollLast(); + int a = numQue.pollLast(); + int op = opQue.pollLast(); + int ans = 0; + switch (op) { + case '+': + ans = a + b; + break; + case '-': + ans = a - b; + break; + case '*': + ans = a * b; + break; + case '/': + ans = a / b; + break; + default: + break; + } + numQue.addLast(ans); + } + + public int calculate(String s) { + s = s.replaceAll(" ", ""); + map.put('+', 0); + map.put('-', 0); + map.put('*', 1); + map.put('/', 1); + map.put('(', -1); + map.put(')', -1); + numQue.addLast(0); + char[] sArr = s.toCharArray(); + int n = sArr.length; + for (int i = 0; i < n; i++) { + // 截取数字 + if (sArr[i] >= '0' && sArr[i] <= '9') { + int start = i; + while (i < n && sArr[i] >= '0' && sArr[i] <= '9') { + ++i; + } + int num = Integer.parseInt(s.substring(start, i)); + numQue.addLast(num); + } + if (i >= n) { + break; + } + // 如果是左括号直接入栈 + if (sArr[i] == '(') { + opQue.addLast(sArr[i]); + // 右括号一直计算知道找到左括号 + } else if (sArr[i] == ')') { + while (opQue.peekLast() != '(') { + compute(); + } + opQue.pollLast(); +// 操作符 + } else { + // 判断是否出现了(- 或者 (+这种情况,那么进行操作数栈补0 + if (i > 0 && sArr[i - 1] == '(') { + numQue.addLast(0); + } + // 如果当前操作符的优先级小于栈顶优先级 + if (!opQue.isEmpty() && map.get(opQue.peekLast()) >= map.get(sArr[i])) { + compute(); + } + opQue.addLast(sArr[i]); + } + + } + + // 如果最后还有元素要继续计算 + while (!opQue.isEmpty()) { + compute(); + } + + return numQue.peekLast(); + + } +} diff --git a/com/zerroi/leetcode/Three22/EvalRPN.java b/com/zerroi/leetcode/Three22/EvalRPN.java new file mode 100644 index 0000000..fe087e7 --- /dev/null +++ b/com/zerroi/leetcode/Three22/EvalRPN.java @@ -0,0 +1,49 @@ +package com.zerroi.leetcode.Three22; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Stack; + +public class EvalRPN { + public static void main(String[] args) { + SolutionFirst solutionFirst = new SolutionFirst(); +// int res = solutionFirst.evalRPN(new String[]{"2", "1", "+", "3", "*"}); + int res = solutionFirst.evalRPN(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"}); + System.out.println("res = " + res); + } +} + +class SolutionFirst { + public int evalRPN(String[] tokens) { + Stack stack = new Stack<>(); + int n1 = 0, n2 = 0; + for (String token : tokens) { + switch (token) { + case "+": + n1 = stack.pop(); + n2 = stack.pop(); + stack.add(n1 + n2); + break; + case "-": + n1 = stack.pop(); + n2 = stack.pop(); + stack.add(n2 - n1); + break; + case "*": + n1 = stack.pop(); + n2 = stack.pop(); + stack.add(n1*n2); + break; + case "/": + n1 = stack.pop(); + n2 = stack.pop(); + stack.add(n2 / n1); + break; + default: + stack.add(Integer.valueOf(token)); + break; + } + } + return stack.isEmpty() ? 0 : stack.getFirst(); + } +} diff --git a/com/zerroi/leetcode/Three22/HasCycle.java b/com/zerroi/leetcode/Three22/HasCycle.java new file mode 100644 index 0000000..3a310f9 --- /dev/null +++ b/com/zerroi/leetcode/Three22/HasCycle.java @@ -0,0 +1,37 @@ +package com.zerroi.leetcode.Three22; + +public class HasCycle { + public static void main(String[] args) { + ListNode l1 = new ListNode(1); + ListNode l2 = new ListNode(1); + l1.next = l2; + SolutionThird solutionThird = new SolutionThird(); + boolean res = solutionThird.hasCycle(l1); + System.out.println("res = " + res); + } +} + +class SolutionThird { + public boolean hasCycle(ListNode head) { + if (head.next == null) return false; + ListNode slow = head; + ListNode fast = head; + while (fast.next != null) { + slow = slow.next; + fast = fast.next.next; + if (slow == fast) { + return true; + } + } + return false; + } +} + +class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + next = null; + } +} diff --git a/com/zerroi/leetcode/Three23/AddTwoNumbers.java b/com/zerroi/leetcode/Three23/AddTwoNumbers.java new file mode 100644 index 0000000..55dde3c --- /dev/null +++ b/com/zerroi/leetcode/Three23/AddTwoNumbers.java @@ -0,0 +1,57 @@ +package com.zerroi.leetcode.Three23; + +public class AddTwoNumbers { + public static void main(String[] args) { + ListNode l1 = new ListNode(2, new ListNode(2, new ListNode(3))); + ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4))); + SolutionFirst solutionFirst = new SolutionFirst(); + ListNode listNode = solutionFirst.addTwoNumbers(l1, l2); + } +} + +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 SolutionFirst { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode head = new ListNode(-1); + ListNode cur = head; + int carry = 0; + int a = 0, b = 0; + int value = 0; + while (l1 != null || l2 != null) { + if (l1 == null) { + a = 0; + } else { + a = l1.val; + } + + if (l2 == null) { + b = 0; + } else { + b = l2.val; + } + value = a + b + carry; + carry = value / 10; + value = value % 10; + cur.next = new ListNode(value); + cur = cur.next; + if (l1 != null) { + l1 = l1.next; + } + if (l2 != null) { + l2 = l2.next; + } + + if (carry != 0) { + cur.next = new ListNode(1); + } + } + return cur; + } +} \ No newline at end of file diff --git a/com/zerroi/leetcode/Three23/MergeTwoLists.java b/com/zerroi/leetcode/Three23/MergeTwoLists.java new file mode 100644 index 0000000..4ee31cd --- /dev/null +++ b/com/zerroi/leetcode/Three23/MergeTwoLists.java @@ -0,0 +1,36 @@ +package com.zerroi.leetcode.Three23; + +public class MergeTwoLists { + public static void main(String[] args) { + ListNode l1 = new ListNode(1, new ListNode(2, new ListNode(4))); + ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4))); + SolutionSecond solutionSecond = new SolutionSecond(); + solutionSecond.mergeTwoLists(l1, l2); + } +} +class SolutionSecond { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + if (list1 == null || list2 == null) { + return list1 == null ? list2 : list1; + } + ListNode pre = new ListNode(-1); + ListNode cur = pre; + while (list1 != null && list2 != null) { + if (list1.val <= list2.val) { + cur.next = list1; + list1 = list1.next; + } else { + cur.next = list2; + list2 = list2.next; + } + cur = cur.next; + if (list1 == null) { + cur.next = list2; + } + if (list2 == null) { + cur.next = list1; + } + } + return pre.next; + } +} diff --git a/com/zerroi/leetcode/Three23/ReverseList.java b/com/zerroi/leetcode/Three23/ReverseList.java new file mode 100644 index 0000000..864be49 --- /dev/null +++ b/com/zerroi/leetcode/Three23/ReverseList.java @@ -0,0 +1,22 @@ +package com.zerroi.leetcode.Three23; + +public class ReverseList { + public static void main(String[] args) { + + } +} + +class SolutionThird { + public ListNode reverseList(ListNode head) { + ListNode pre = null; + ListNode cur = head; + ListNode temp = null; + while (cur != null) { + temp = cur.next; + cur.next = pre; + pre = cur; + cur = temp; + } + return pre; + } +} diff --git a/com/zerroi/leetcode/Three24/ReverseBetween.java b/com/zerroi/leetcode/Three24/ReverseBetween.java new file mode 100644 index 0000000..9b63392 --- /dev/null +++ b/com/zerroi/leetcode/Three24/ReverseBetween.java @@ -0,0 +1,52 @@ +package com.zerroi.leetcode.Three24; + +public class ReverseBetween { + public static void main(String[] args) { + + } +} + +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 (left == right) return head; + ListNode dummy1 = new ListNode(-1); + ListNode dummy2 = new ListNode(-1); + ListNode cur1 = dummy1; + ListNode cur2 = dummy2; + for (int i = 0; i < left - 1; i++) { + cur1.next = new ListNode(head.val); + cur1 = cur1.next; + head = head.next; + } + for (int i = left; i <= right; i++) { + cur2.next = new ListNode(head.val); + cur2 = cur2.next; + head = head.next; + } + ListNode reversed = reverse(dummy2.next); + cur1.next = reversed; + cur2.next = head; + return dummy1.next; + + } + private ListNode reverse(ListNode head) { + ListNode pre = null; + ListNode cur = head; + ListNode temp; + while (cur != null) { + temp = cur.next; + cur.next = pre; + pre = cur; + cur = temp; + } + return pre; + } +} diff --git a/com/zerroi/leetcode/Three25/DeleteDuplicates.java b/com/zerroi/leetcode/Three25/DeleteDuplicates.java new file mode 100644 index 0000000..b01fa45 --- /dev/null +++ b/com/zerroi/leetcode/Three25/DeleteDuplicates.java @@ -0,0 +1,56 @@ +package com.zerroi.leetcode.Three25; + +public class DeleteDuplicates { + public static void main(String[] args) { +// ListNode head = constructLinkedList(new int[]{1, 2, 3, 3, 4, 4, 5}); + ListNode head = constructLinkedList(new int[]{1, 1}); + SolutionSecond solutionSecond = new SolutionSecond(); + ListNode res = solutionSecond.deleteDuplicates(head); + while (res != null) { + System.out.println("res.val = " + res.val); + res = res.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; // 返回真正的头节点 + } +} + +class SolutionSecond { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) return head; + ListNode dummy = new ListNode(-1); + dummy.next = head; +// pre指针为cur的前驱节点 + ListNode pre = dummy; +// cur为当前节点 + ListNode cur = head; + while (cur != null && cur.next != null) { +// 如果出现当前节点的值等于其下一节点值,要一直循环找到不相等的 + if (cur.val == cur.next.val) { + int duplicate = cur.val; + while (cur != null && cur.val == duplicate) { + cur = cur.next; + } + pre.next = cur; + } else { + pre = pre.next; + cur = cur.next; + } + } + return dummy.next; + } +} diff --git a/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java b/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java new file mode 100644 index 0000000..93bdcad --- /dev/null +++ b/com/zerroi/leetcode/Three25/RemoveNthFromEnd.java @@ -0,0 +1,55 @@ +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)); + 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; + } +} + +class SolutionFirst { + public ListNode removeNthFromEnd(ListNode head, int n) { + if(head == null) return null; + ListNode cur = head; + int length = length(head); + if(length == 1) return null; + if (length == n) return head.next; + + ListNode dummy = new ListNode(0, head); + for (int i = 1; i <= length - n; i++) { + cur = cur.next; + dummy = dummy.next; + } + dummy.next = cur.next; + + return head; + } + + private int length(ListNode head) { + int length = 0; + while (head != null) { + length++; + head = head.next; + } + return length; + } +} diff --git a/com/zerroi/leetcode/ThreeFive/Rotate.java b/com/zerroi/leetcode/ThreeFive/Rotate.java index 530301f..53dd910 100644 --- a/com/zerroi/leetcode/ThreeFive/Rotate.java +++ b/com/zerroi/leetcode/ThreeFive/Rotate.java @@ -15,17 +15,13 @@ class SolutionSecond { public void rotate(int[] nums, int k) { k = k % nums.length; int[] temp = new int[k]; - for (int i = 0; i < k; i++) { - temp[i] = nums[nums.length - k + i]; - } + System.arraycopy(nums, nums.length - k, temp, 0, k); for (int i = nums.length - k; i >= 0; i--) { nums[i + k] = nums[i]; } - for (int i = 0; i < k; i++) { - nums[i] = temp[i]; - } + System.arraycopy(temp, 0, nums, 0, k); } public void rotateReverse(int[] nums, int k) {