郑州网站优化顾问,网站建设如何制作教程,wordpress后台图,房地产销售营销方案本系列为笔者的 Leetcode 刷题记录#xff0c;顺序为 Hot 100 题官方顺序#xff0c;根据标签命名#xff0c;记录笔者总结的做题思路#xff0c;附部分代码解释和疑问解答#xff0c;01~07为C语言#xff0c;08及以后为Java语言。
01 有效的括号 class Solution {publi…本系列为笔者的 Leetcode 刷题记录顺序为 Hot 100 题官方顺序根据标签命名记录笔者总结的做题思路附部分代码解释和疑问解答01~07为C语言08及以后为Java语言。
01 有效的括号 class Solution {public boolean isValid(String s) {int n s.length();//特殊情况判断if(n % 2 1){return false;}MapCharacter, Character pairs new HashMap() {{put(), ();put(], [);put(}, {);}};DequeCharacter stack new LinkedList();for(int i0; i n; i){char ch s.charAt(i);if(pairs.containsKey(ch)){ //右括号做判断if(stack.isEmpty() || stack.peek() ! pairs.get(ch)){return false;}stack.pop();}else{ //左括号压入栈stack.push(ch);}}return stack.isEmpty();}
}02 最小栈 class MinStack {public MinStack() {}public void push(int val) {}public void pop() {}public int top() {}public int getMin() {}
}/*** Your MinStack object will be instantiated and called as such:* MinStack obj new MinStack();* obj.push(val);* obj.pop();* int param_3 obj.top();* int param_4 obj.getMin();*/方法辅助栈
class MinStack {DequeInteger xStack;DequeInteger minStack;public MinStack() {xStack new LinkedList();minStack new LinkedList();minStack.push(Integer.MAX_VALUE);}public void push(int val) {xStack.push(val);minStack.push(Math.min(minStack.peek(), val));}public void pop() {xStack.pop();minStack.pop();}public int top() {return xStack.peek();}public int getMin() {return minStack.peek();}
}/*** Your MinStack object will be instantiated and called as such:* MinStack obj new MinStack();* obj.push(val);* obj.pop();* int param_3 obj.top();* int param_4 obj.getMin();*/03 字符串解码 class Solution {int ptr;public String decodeString(String s) {LinkedListString stk new LinkedList();ptr 0;while(ptr s.length()){char cur s.charAt(ptr);if(Character.isDigit(cur)){String digits getDigit(s);stk.addLast(digits); //1. 添加数字}else if(Character.isLetter(cur) || cur [){stk.addLast(String.valueOf(s.charAt(ptr))); //2.添加单个字符}else{//获取括号中的字符串ptr;LinkedListString sub new LinkedList();while(![.equals(stk.peekLast())){sub.addLast(stk.removeLast());}Collections.reverse(sub);stk.removeLast(); //弹出左括号int time Integer.parseInt(stk.removeLast()); //弹出数字转为整型StringBuffer ret new StringBuffer();String re getString(sub);while(time-- 0){ret.append(re);}stk.addLast(ret.toString()); //3.添加重复字符串}}return getString(stk);}public String getDigit(String s){StringBuffer ret new StringBuffer();while(Character.isDigit(s.charAt(ptr))){ret.append(s.charAt(ptr));}return ret.toString();}public String getString(LinkedListString v){StringBuffer ret new StringBuffer();for(String s : v){ret.append(s);}return ret.toString();}
}04 每日温度 class Solution {public int[] dailyTemperatures(int[] temperatures) {int length temperatures.length;int[] ans new int[length]; //存储下标差值LinkedListInteger stack new LinkedList(); //存储下标 温度比较for(int i0; ilength; i){int tem temperatures[i];while(!stack.isEmpty() tem temperatures[stack.peek()]){int preIndex stack.pop(); ans[preIndex] i - preIndex; //⭐}stack.push(i);}return ans;}
}05 柱状图的最大矩形 class Solution {public int largestRectangleArea(int[] heights) {//1、创建stack、maxAreaint n heights.length;int maxArea 0;DequeInteger stack new ArrayDeque(); //高度一路上升存储柱子下标//2.for循环、遍历柱子下标for(int i0; in; i){int currHeight (i n) ? 0 : heights[i];while(!stack.isEmpty() currHeight heights[stack.peek()]){int height heights[stack.pop()]; //⭐int width stack.isEmpty() ? i : i - stack.peek() - 1;maxArea Math.max(maxArea, height * width);}stack.push(i);}//3.返回最大面积return maxArea;}
}