找回密码
 立即注册
首页 业界区 业界 hot100之栈

hot100之栈

凤清昶 2025-9-25 20:00:02
有效的括号(020)

跳过
最小栈(155)
  1. class MinStack {
  2.     private final Deque<int[]> stack = new ArrayDeque<>();
  3.     public MinStack() {
  4.         stack.addLast(new int[]{0, Integer.MAX_VALUE});
  5.     }
  6.     public void push(int val) {
  7.         stack.addLast(new int[]{val, Math.min(stack.peekLast()[1], val)});
  8.     }
  9.     public void pop() {
  10.         stack.removeLast();
  11.     }
  12.     public int top() {
  13.         return stack.peekLast()[0];
  14.     }
  15.     public int getMin() {
  16.         return stack.peekLast()[1];
  17.     }
  18. }
复制代码

  • 分析
使用双端队列作栈
利用动态规划, 每个栈元素维护自身val和min_val
字符串解码(394)
  1. class Solution {
  2.     public String decodeString(String s) {
  3.         int idx = 0;
  4.         StringBuilder builder = new StringBuilder();
  5.         while (idx < s.length()){
  6.             // 是字母
  7.             if (s.charAt(idx) >= 'a'){
  8.                 builder.append(s.charAt(idx));
  9.                 idx++;
  10.                 continue;
  11.             }
  12.             idx = appendDupString(idx, s, builder);
  13.             //是数字
  14.         }
  15.         return builder.toString();
  16.     }
  17.     private int appendDupString(int idx, String s , StringBuilder builder){
  18.         int prefix_count = 0;
  19.         while(s.charAt(idx) != '['){
  20.             prefix_count = prefix_count * 10 + s.charAt(idx) - '0';
  21.             idx++;
  22.         }
  23.         int rig_idx = idx+1;
  24.         int nest = 1;
  25.         while (nest != 0){
  26.             if (s.charAt(rig_idx) == '[') nest++;
  27.             else if (s.charAt(rig_idx) == ']') nest--;
  28.             rig_idx++;
  29.         }
  30.         String subString = decodeString(s.substring(idx+1, rig_idx-1));
  31.         for (int i = 0; i < prefix_count; i++){
  32.             builder.append(subString);
  33.         }
  34.         return rig_idx;
  35.     }
  36. }
复制代码

  • 分析
递归调用
每日温度(739)

栈解法
  1. class Solution {
  2.     public int[] dailyTemperatures(int[] temperatures) {
  3.         int n = temperatures.length;
  4.         int[] res = new int[n];
  5.         Deque<Integer> stack = new ArrayDeque<>();
  6.         for (int i = n-1; i >= 0; i--){
  7.             int t = temperatures[i];
  8.             while(!stack.isEmpty() && t >=temperatures[stack.peek()]){
  9.                 stack.pop();
  10.             }
  11.             if (!stack.isEmpty()){
  12.                 res[i] = stack.peek() - i;
  13.             }
  14.             stack.push(i);
  15.         }
  16.         return res;
  17.     }
  18. }
复制代码
跳表解法
  1. class Solution {
  2.     public int[] dailyTemperatures(int[] temperatures) {
  3.         int n = temperatures.length;
  4.         int[] res = new int[n];
  5.         for (int i = n-2; i >= 0; i--){
  6.             for (int j = i+1; j < n; j += res[j]){
  7.                 if (temperatures[j] > temperatures[i]){
  8.                     res[i] = j - i;
  9.                     break;
  10.                 }
  11.                 else if (res[j] == 0){
  12.                     res[i] = 0;
  13.                     break;
  14.                 }
  15.             }
  16.         }
  17.         return res;
  18.     }
  19. }
复制代码

  • 分析
栈解法
栈用于维护数据
维护栈的单调性就是在维护栈组中的数据的性, 栈自身的性质可以用于保持特性
跳表解法
可能也许这是贪心
柱状图中最大矩形(084)

[code]class Solution {    public int largestRectangleArea(int[] heights) {        int n = heights.length;        Deque stack = new ArrayDeque();        stack.push(-1);        int res = 0;        for (int rig = 0; rig  1 && h

相关推荐

您需要登录后才可以回帖 登录 | 立即注册