加油冲
This commit is contained in:
parent
2e3d5486bc
commit
cca9883795
|
@ -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<Character> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -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<String> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Character, Integer> map = new HashMap<>();
|
||||||
|
private Deque<Character> opQue = new LinkedList<>();
|
||||||
|
private Deque<Integer> 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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Integer> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,17 +15,13 @@ class SolutionSecond {
|
||||||
public void rotate(int[] nums, int k) {
|
public void rotate(int[] nums, int k) {
|
||||||
k = k % nums.length;
|
k = k % nums.length;
|
||||||
int[] temp = new int[k];
|
int[] temp = new int[k];
|
||||||
for (int i = 0; i < k; i++) {
|
System.arraycopy(nums, nums.length - k, temp, 0, k);
|
||||||
temp[i] = nums[nums.length - k + i];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = nums.length - k; i >= 0; i--) {
|
for (int i = nums.length - k; i >= 0; i--) {
|
||||||
nums[i + k] = nums[i];
|
nums[i + k] = nums[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < k; i++) {
|
System.arraycopy(temp, 0, nums, 0, k);
|
||||||
nums[i] = temp[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rotateReverse(int[] nums, int k) {
|
public void rotateReverse(int[] nums, int k) {
|
||||||
|
|
Loading…
Reference in New Issue