堆栈
在Java中可用ArrayDeque来模拟堆栈数据结构
例题
leetcode20
class Solution { public boolean isValid(String s) { ArrayDeque<Character> stack = new ArrayDeque<>(); for(int i = 0 , len = s.length(); i < len ; i++){ if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{'){ stack.addFirst(s.charAt(i)); }else{ if(stack.isEmpty())return false; switch(s.charAt(i)){ case ')': if(stack.getFirst() != '(')return false; stack.removeFirst(); break; case ']': if(stack.getFirst() != '[')return false; stack.removeFirst(); break; case '}': if(stack.getFirst() != '{')return false; stack.removeFirst(); break; } } } if(!stack.isEmpty())return false; return true; } }
leetcode32
class Solution { public int longestValidParentheses(String s) { ArrayDeque<Integer> stack = new ArrayDeque<>(); // 记录(的下标 int MAX = 0; int len = s.length(); int [] dp = new int[len]; //dp[i]记录以以s[i]结尾的最长的括号数 for (int i = 0; i < len ; i++){ if(s.charAt(i) == '('){ stack.addFirst(i); }else{ if(stack.isEmpty()){ continue; } int index = stack.getFirst(); int clen; if(index != 0){ clen = i - index + 1 + dp[index - 1]; }else { clen = i - index + 1; } dp[i] = clen; stack.removeFirst(); MAX = Integer.max(MAX,dp[i]); } } return MAX; } }
leetcode225
class MyStack { ArrayDeque<Integer> stack ; public MyStack() { stack = new ArrayDeque<>(); } public void push(int x) { stack.addFirst(x); } public int pop() { int res = stack.getFirst(); stack.removeFirst(); return res; } public int top() { return stack.getFirst(); } public boolean empty() { return stack.isEmpty(); } } /** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
leetcode232
class MyQueue { Stack<Integer> s1 = new Stack<>(); Stack<Integer> s2 = new Stack<>(); public MyQueue() { } public void push(int x) { s1.push(x); } public int pop() { while(!s1.isEmpty()){ int t = s1.peek(); s1.pop(); s2.push(t); } int res = s2.peek(); s2.pop(); while(!s2.isEmpty()){ int t = s2.peek(); s2.pop(); s1.push(t); } return res; } public int peek() { while(!s1.isEmpty()){ int t = s1.peek(); s1.pop(); s2.push(t); } int res = s2.peek(); while(!s2.isEmpty()){ int t = s2.peek(); s2.pop(); s1.push(t); } return res; } public boolean empty() { return s1.isEmpty(); } } /** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
leetcode239
// 单调栈的思想
// 滑动窗口大小的单调栈,进行移动的时候看移除元素是否在栈顶,如果是的话则移除,不是的话则继续进行添加!
class Solution { public int[] maxSlidingWindow(int[] nums, int k) { LinkedList<Integer> stack = new LinkedList<>(); // 维护一个单调递减的栈 int cv = 0; int len = nums.length; int index = 0; int []res = new int [len - k + 1]; for (int i = 0 ; i<len ; i++){ if(cv < k - 1){ while(!stack.isEmpty()&&stack.getFirst()<nums[i]){ stack.removeFirst(); } stack.addFirst(nums[i]); cv++; }else if(cv == k - 1){ while(!stack.isEmpty()&&stack.getFirst()<nums[i]){ stack.removeFirst(); } stack.addFirst(nums[i]); cv++; res[index] = stack.getLast(); index ++; if(stack.getLast() == nums[i - k + 1]){ stack.removeLast(); } cv --; } } return res; } }
leetcode921
class Solution { public int minAddToMakeValid(String s) { int left = 0;// 左括号 int res = 0; for (int i = 0 ,len = s.length(); i < len ; i++){ if(s.charAt(i) == ')'){ if(left == 0){ res++; }else { left --; } }else { left ++; } } return res+left; } }
leetcode1541
class Solution { public int minInsertions(String s) { // 一个左括号对应两个连续的右括号 int res =0; int left = 0; for (int i = 0 ,len = s.length(); i < len ; i++){ if(s.charAt(i) == '('){ left ++; }else{ // 单个右括号 if(i == len-1){ res += 1; if(left != 0) left --; else res++; }else{ if(s.charAt(i+1) == ')'){ i++; if(left != 0) left --; else res++; }else{ res += 1; if(left != 0) left --; else res++; } } // 两个右括号 } } return res+2*left; } }