有效的括号
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 s
,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
示例 1:
输入:s = "()" 输出:true
示例 2:
输入:s = "()[]{}" 输出:true
示例 3:
输入:s = "(]" 输出:false
示例 4:
输入:s = "([)]" 输出:false
示例 5:
输入:s = "{[]}" 输出:true
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}
最长有效括号
给你一个只包含 '('
和 ')'
的字符串,找出最长有效(格式正确且连续)括号子串的长度。
示例 1:
输入:s = "(()" 输出:2 解释:最长有效括号子串是 "()"
示例 2:
输入:s = ")()())" 输出:4 解释:最长有效括号子串是 "()()"
示例 3:
输入:s = "" 输出:0
public class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<Integer>();
int max=0;
int left = -1;
for(int j=0;j<s.length();j++){
if(s.charAt(j)=='(') stack.push(j);
else {
if (stack.isEmpty()) left=j;
else{
stack.pop();
if(stack.isEmpty()) max=Math.max(max,j-left);
else max=Math.max(max,j-stack.peek());
}
}
}
return max;
}
}
接雨水
给定 n
个非负整数表示每个宽度为 1
的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
class Solution {
public int trap(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int left = 0; int right = height.length - 1; // Pointers to both ends of the array.
int maxLeft = 0; int maxRight = 0;
int totalWater = 0;
while (left < right) {
// Water could, potentially, fill everything from left to right, if there is nothing in between.
if (height[left] < height[right]) {
// If the current elevation is greater than the previous maximum, water cannot occupy that point at all.
// However, we do know that everything from maxLeft to the current index, has been optimally filled, as we've
// been adding water to the brim of the last maxLeft.
if (height[left] >= maxLeft) {
// So, we say we've found a new maximum, and look to see how much water we can fill from this point on.
maxLeft = height[left];
// If we've yet to find a maximum, we know that we can fill the current point with water up to the previous
// maximum, as any more will overflow it. We also subtract the current height, as that is the elevation the
// ground will be at.
} else {
totalWater += maxLeft - height[left];
}
// Increment left, we'll now look at the next point.
left++;
// If the height at the left is NOT greater than height at the right, we cannot fill from left to right without over-
// flowing; however, we do know that we could potentially fill from right to left, if there is nothing in between.
} else {
// Similarly to above, we see that we've found a height greater than the max, and cannot fill it whatsoever, but
// everything before is optimally filled
if (height[right] >= maxRight) {
// We can say we've found a new maximum and move on.
maxRight = height[right];
// If we haven't found a greater elevation, we can fill the current elevation with maxRight - height[right]
// water.
} else {
totalWater += maxRight - height[right];
}
// Decrement left, we'll look at the next point.
right--;
}
}
// Return the sum we've been adding to.
return totalWater;
}
}