This commit is contained in:
fleyx 2024-02-19 23:40:06 +08:00
parent f72bb3069a
commit ff9af65feb
4 changed files with 152 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package com.fanxb.common; package com.fanxb.common;
import java.util.Stack;
public class Q150 { public class Q150 {
public void gameOfLife(int[][] board) { public void gameOfLife(int[][] board) {
int m = board.length, n = board[0].length; int m = board.length, n = board[0].length;
@ -32,6 +34,31 @@ public class Q150 {
} }
} }
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String str : tokens) {
switch (str) {
case "+":
stack.push(stack.pop() + stack.pop());
break;
case "-":
stack.push(-stack.pop() + stack.pop());
break;
case "*":
stack.push(stack.pop() * stack.pop());
break;
case "/":
int num1 = stack.pop(), num2 = stack.pop();
stack.push(num2 / num1);
break;
default:
stack.push(Integer.valueOf(str));
}
}
return stack.pop();
}
public static void main(String[] args) { public static void main(String[] args) {
} }
} }

View File

@ -0,0 +1,28 @@
package com.fanxb.common;
import java.util.LinkedList;
public class Q155 {
private LinkedList<Integer[]> stack = new LinkedList<>();
public Q155() {
}
public void push(int val) {
int min = stack.isEmpty() ? val : Math.min(stack.getFirst()[1], val);
stack.push(new Integer[]{val, min});
}
public void pop() {
stack.removeFirst();
}
public int top() {
return stack.getFirst()[0];
}
public int getMin() {
return stack.getFirst()[1];
}
}

View File

@ -0,0 +1,72 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Q224 {
public int calculate(String s) {
Stack<String> stack = new Stack<>();
int len = s.length();
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
case ' ':
break;
case '(':
case '+':
case '-':
stack.push(ch + "");
break;
case ')':
List<String> strs = new ArrayList<>();
String temp;
while (!(temp = stack.pop()).equals("("))
strs.add(temp);
stack.push(cal(strs).toString());
break;
default:
//找到数字
StringBuilder builder = new StringBuilder().append(ch);
for (i = i + 1; i < len; i++) {
char ca = s.charAt(i);
if (ca >= '0' && ca <= '9') builder.append(ca);
else break;
}
i--;
stack.push(builder.toString());
}
}
List<String> strings = new ArrayList<>();
while (!stack.isEmpty())
strings.add(stack.pop());
return cal(strings);
}
private Integer cal(List<String> strs) {
int len = strs.size();
if (len == 1) return Integer.valueOf(strs.get(0));
if (len == 2) return Integer.valueOf(strs.get(1) + strs.get(0));
String first = strs.get(len - 1);
int i, num1;
if (first.equals("-")) {
num1 = -1 * Integer.parseInt(strs.get(len - 2));
i = len - 3;
} else {
num1 = Integer.parseInt(first);
i = len - 2;
}
for (; i >= 0; i -= 2) {
if (strs.get(i).equals("+")) {
num1 += Integer.parseInt(strs.get(i - 1));
} else {
num1 -= Integer.parseInt(strs.get(i - 1));
}
}
return num1;
}
public static void main(String[] args) {
System.out.println(new Q224().calculate("-2+1"));
}
}

View File

@ -0,0 +1,25 @@
package com.fanxb.common;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Q71 {
public String simplifyPath(String path) {
Stack<String> stack = new Stack<>();
List<String> strs = Stream.of(path.split("/+")).filter(item -> !item.isEmpty()).collect(Collectors.toList());
for (String str : strs) {
switch (str) {
case ".":
break;
case "..":
if (!stack.isEmpty()) stack.pop();
break;
default:
stack.push(str);
}
}
return stack.isEmpty() ? "/" : stack.stream().map(item -> "/" + item).collect(Collectors.joining());
}
}