LeetCode算法练习top100:(9)栈和堆
发布时间:2023年12月17日
package top100.栈堆;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.PriorityQueue;
import java.util.Stack;
public class TOP {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(')');
} else if (s.charAt(i) == '[') {
stack.push(']');
} else if (s.charAt(i) == '{') {
stack.push('}');
} else {
if (stack.isEmpty() || stack.peek() != s.charAt(i)) {
return false;
} else {
stack.pop();
}
}
}
return stack.isEmpty();
}
class MinStack {
Stack<Integer> num;
Stack<Integer> min;
public MinStack() {
num = new Stack<>();
min = new Stack<>();
}
public void push(int val) {
num.push(val);
if (min.isEmpty() || val <= min.peek()) {
min.push(val);
} else {
min.push(min.peek());
}
}
public void pop() {
num.pop();
min.pop();
}
public int top() {
return num.peek();
}
public int getMin() {
return min.peek();
}
}
int index;
public String decodeString(String s) {
index = 0;
return dfs(s);
}
private String dfs(String s) {
StringBuilder sb = new StringBuilder();
int k = 0;
while (index < s.length()) {
char c = s.charAt(index++);
if (c >= '0' && c <= '9') {
k = k * 10 + c - '0';
} else if (c == '[') {
String str = dfs(s);
while (k > 0) {
sb.append(str);
k--;
}
} else if (c == ']') {
break;
} else {
sb.append(c);
}
}
return sb.toString();
}
public String decodeString(String s) {
Deque<Integer> stackDigit = new ArrayDeque<>();
Deque<String> stackStr = new ArrayDeque<>();
int curK = 0;
StringBuilder curStr = new StringBuilder();
for (Character c : s.toCharArray()) {
if (c == '[') {
stackDigit.push(curK);
stackStr.push(curStr.toString());
curK = 0;
curStr = new StringBuilder();
} else if (c == ']') {
int k = stackDigit.pop();
StringBuilder temp = new StringBuilder();
while (k > 0) {
temp.append(curStr);
k--;
}
curStr = new StringBuilder(stackStr.pop() + temp);
} else if (c >= '0' && c <= '9') {
curK = curK * 10 + c - '0';
} else {
curStr.append(c);
}
}
return curStr.toString();
}
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] res = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
int cur = temperatures[i];
while (!stack.isEmpty() && cur > temperatures[stack.peek()]) {
int pre = stack.pop();
res[pre] = i - pre;
}
stack.push(i);
}
return res;
}
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] res = new int[n];
for (int i = temperatures.length - 2; i >= 0; i--) {
int j = i + 1;
while (j < temperatures.length) {
if (temperatures[j] > temperatures[i]) {
res[i] = j - i;
break;
} else if (res[j] == 0) {
break;
} else {
j += res[j];
}
}
}
return res;
}
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int num : nums) {
q.offer(num);
if (q.size() > k) {
q.poll();
}
}
return q.peek();
}
class MedianFinder {
PriorityQueue<Integer> min, max;
public MedianFinder() {
min = new PriorityQueue<>((o1, o2) -> o2 - o1);
max = new PriorityQueue<>((o1, o2) -> o1 - o2);
}
public void addNum(int num) {
if (min.isEmpty() || num < min.peek()) {
min.offer(num);
if (min.size() - max.size() > 1) {
max.offer(min.poll());
}
} else {
max.offer(num);
if (max.size() - min.size() > 0) {
min.offer(max.poll());
}
}
}
public double findMedian() {
if (min.size() != max.size()) {
return min.peek();
} else {
return (min.peek() + max.peek()) / 2.0;
}
}
}
}
文章来源:https://blog.csdn.net/weixin_42774617/article/details/135046348
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!