使用map和stack结合.
map中的key是右括号,value是左括号。
stack中存放所有的左括号
最后只需要判断栈是否为空即可。
代码
class Solution {
public boolean isValid(String s) {
int n = s.length();
if(n % 2 == 1){
return false;
}
Map<Character, Character> map = new HashMap<>();
map.put(')','(');
map.put(']','[');
map.put('}','{');
Deque<Character> stack = new LinkedList<>();
for(int i = 0; i < n; i++){
char c = s.charAt(i);
if(map.containsKey(c)){
if(stack.isEmpty() || stack.peek() != map.get(c)){
return false;
}else{
stack.pop();
}
}else {
stack.push(c);
}
}
return stack.isEmpty();
}
}
设置一个栈,先放入一个-1。保证是一个单调栈(主要是防止当第一个字符是")"进行pop的时候,出现空指针)。
依次扫描
返回最大值。
代码
class Solution {
public int longestValidParentheses(String s) {
int n = s.length();
int res = 0;
Deque<Integer> stack = new LinkedList<>();
stack.push(-1);
for(int i = 0; i < n; i++){
char c = s.charAt(i);
if(c == '('){
stack.push(i);
}else {
stack.pop();
if(stack.isEmpty()){
stack.push(i);
}else {
res = Math.max(res, i - stack.peek());
}
}
}
return res;
}
}
while
判断,这样一直找到最远的那个墙(可以接雨水的),栈是否为空以及栈顶对应height数组的值是否大于现在扫描的height值。如果当前height值大于栈顶height值,那么说明出现了凹槽了,可以存放雨水。
代码
class Solution {
public int trap(int[] height) {
int n = height.length;
int res = 0;
Deque<Integer> stack = new LinkedList<>();
for(int i = 0; i < n; i++){
while(!stack.isEmpty() && height[i] > height[stack.peek()]){
//弹出凹槽部分(可能是空槽,也可能是矮墙)
int top = stack.pop();
if(stack.isEmpty()){
break;
}
int left = stack.peek();
int widthNow = i - stack.peek() - 1;
//高度为当前高度和左边墙的高度的最小值,再减去凹槽
int heightNow = Math.min(height[i],height[left]) - height[top];
res += widthNow * heightNow;
}
stack.push(i);
}
return res;
}
}
代码:
class Solution {
public int largestRectangleArea(int[] heights) {
int[] temp = new int[heights.length + 2];
System.arraycopy(heights, 0, temp, 1, heights.length);
Deque<Integer> stack = new LinkedList<>();
int res = 0;
for(int i = 0; i < temp.length; i++){
//判断条件是当前位置的高度小于栈顶的高度,记住是循环
while(!stack.isEmpty() && temp[i] < temp[stack.peek()]){
int left = stack.pop();
//h选择左边的高度。
int h = temp[left];
int w = i - stack.peek() - 1;
res = Math.max(res, w * h);
}
stack.push(i);
}
return res;
}
}
代码:
class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0){
return 0;
}
int[] heights = new int[matrix[0].length];
int maxArea = 0;
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
if(matrix[i][j] == '1'){
heights[j] += 1;
}else {
//因为题目要求只保留全为1的
heights[j] = 0;
}
}
//每一行求一次
maxArea = Math.max(maxArea, largestRectangleArea(heights));
}
return maxArea;
}
public int largestRectangleArea(int[] heights){
int[] temp = new int[heights.length + 2];
System.arraycopy(heights, 0 ,temp, 1, heights.length);
Deque<Integer> stack = new LinkedList<>();
int res = 0;
for(int i = 0; i < temp.length; i++){
while(!stack.isEmpty() && temp[i] < temp[stack.peek()]){
int left = stack.pop();
int h = temp[left];
int w = i - stack.peek() - 1;
res = Math.max(res, w * h);
}
stack.push(i);
}
return res;
}
}
代码:
class MinStack {
Deque<Integer> data;
Deque<Integer> minData;
/** initialize your data structure here. */
public MinStack() {
data = new LinkedList<>();
minData = new LinkedList<>();
}
public void push(int val) {
data.push(val);
if(minData.isEmpty() || minData.peek() >= val){
minData.push(val);
}else {
minData.push(minData.peek());
}
}
public void pop() {
data.pop();
minData.pop();
}
public int top() {
return data.peek();
}
public int getMin() {
return minData.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();
*/
代码
class Solution {
public String decodeString(String s) {
Deque<Integer> numStack = new LinkedList<>();
Deque<String> strStack = new LinkedList<>();
StringBuffer sb = new StringBuffer();
int num = 0;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
num = num * 10 + (c - '0');
}else if(Character.isLetter(c)){
sb.append(c);
}else if(c == '['){
strStack.push(sb.toString());
sb = new StringBuffer();
numStack.push(num);
num = 0;
}else if(c == ']'){
int repeat = numStack.pop();
StringBuffer newStr = new StringBuffer();
newStr.append(strStack.pop());
for(int j = 0; j < repeat; j++){
newStr.append(sb.toString());
}
sb = newStr;
}
}
return sb.toString();
}
}
代码:
class Solution {
public int findUnsortedSubarray(int[] nums) {
Deque<Integer> stack = new LinkedList<>();
int n = nums.length;
int l = n;
int r = 0;
for(int i = 0; i < n; i++){
while(!stack.isEmpty() && nums[i] < nums[stack.peek()]){
int newLeft = stack.pop();
l = Math.min(l, newLeft);
}
stack.push(i);
}
stack.clear();
for(int i = n - 1; i >= 0; i--){
while(!stack.isEmpty() && nums[i] > nums[stack.peek()]){
int newRight = stack.pop();
r = Math.max(r, newRight);
}
stack.push(i);
}
if(r > l){
return r - l + 1;
}else {
return 0;
}
}
}
代码
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
Deque<Integer> stack = new LinkedList<>();
int n = temperatures.length;
int[] res = new int[n];
for(int i = 0; i < n; i++){
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
int index = stack.pop();
res[index] = i - index;
}
stack.push(i);
}
return res;
}
}
代码:
class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
/** Get the front element. */
public int peek() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack2.isEmpty() && stack1.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();
*/