括号常考面试题
Posted 赵jc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了括号常考面试题相关的知识,希望对你有一定的参考价值。
括号常考面试题
括号匹配
https://leetcode-cn.com/problems/valid-parentheses/
class Solution
public boolean isValid(String s)
if(s == null || s.length() == 0)
return false;
int len = s.length();
for(int i = 0; i < len; i++)
s = s.replace("()","").replace("[]","").replace("","");
return s.length() == 0;
class Solution
public boolean isValid(String s)
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length();i++)
char ch = s.charAt(i);
if(ch == ''||ch == '['||ch == '(')
stack.push(ch);
else
if(stack.isEmpty())
return false;
char top = stack.peek();
if(top == '' && ch =='' || top == '[' && ch ==']'|| top == '(' && ch ==')')
stack.pop();
else
return false;
if(stack.empty())
return true;
return false;
带*的括号匹配
https://leetcode-cn.com/problems/valid-parenthesis-string/
class Solution
//记录当前未匹配左括号数量的范围即可。只需遍历一遍,不需要使用栈。
public boolean checkValidString(String s)
if(s == null || s.length() == 0)
return true;
int l = 0, r = 0;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == '(')
l++;
r++;
else if (s.charAt(i) == ')')
l--;
r--;
else
l--;
r++;
if (r < 0)
return false;
if (l < 0) l = 0;
return l == 0;
//双栈实现
class Solution
public boolean checkValidString(String s)
//存储左括号索引
Stack<Integer> leftStack = new Stack<>();
//存储*索引
Stack<Integer> starStack = new Stack<>();
int n = s.length();
for (int i = 0; i < n; i++)
char c = s.charAt(i);
if (c == '(')
leftStack.push(i);
else if (c == '*')
starStack.push(i);
else
if (!leftStack.isEmpty())
leftStack.pop();
else if (!starStack.isEmpty())
starStack.pop();
else
return false;
while (!leftStack.isEmpty() && !starStack.isEmpty())
//把*当成右括号,但是必须右括号的索引 > 左括号才能匹配
//因为是栈结构,所以根据遍历,栈顶元素的索引是最大的索引,如果不满足就return false
if (leftStack.pop() > starStack.pop())
return false;
return leftStack.isEmpty();
括号生产(生成匹配的括号)
https://leetcode-cn.com/problems/generate-parentheses/
class Solution
List<String> ret = new ArrayList<>();
public List<String> generateParenthesis(int n)
dfs(n, n, "");
return ret;
private void dfs(int left, int right, String curStr)
if (left == 0 && right == 0) // 左右括号都不剩余了,递归终止
ret.add(curStr);
return;
if (left > 0) // 如果左括号还剩余的话,可以拼接左括号
dfs(left - 1, right, curStr + "(");
if (right > left) // 如果右括号剩余多于左括号剩余的话,可以拼接右括号
dfs(left, right - 1, curStr + ")");
括号生成II(面试题)
https://leetcode-cn.com/problems/bracket-lcci/
class Solution
List<String> ret = new ArrayList<String>();
public List<String> generateParenthesis(int n)
String s = "";
int l = 0;
int r = 0;
dfs(n, s, l, r);
return ret;
public void dfs(int n, String s, int l, int r)
// 终止条件
if(s.length() == 2 * n)
ret.add(s);
return ;
// l < n 则可添加左括号
if(l < n)
dfs(n, s + "(", l + 1, r);
// 如果该位置的右括号数目小于左括号 则可添加右括号
if(r < l)
dfs(n, s + ")", l, r + 1);
最长括号子串(dp)
import java.util.*;
public class Solution
public int longestValidParentheses (String s)
// write code here
if (s == null || s.length() < 2)
return 0;
int[] f = new int[s.length()];
if (s.charAt(0) == '(' && s.charAt(1) == ')')
f[1] = 2;
int ret = Math.max(f[1], 0);
for (int i = 2; i < s.length(); i++)
if (s.charAt(i) == ')')
if (s.charAt(i - 1) == '(')
f[i] = 2 + f[i - 2];
else
int index = i - f[i - 1] - 1;
if (index >= 0 && s.charAt(index) == '(')
f[i] = 2 + f[i - 1];
if (index - 1 >= 0)
f[i] += f[index - 1];
ret = Math.max(ret, f[i]);
return ret;
以上是关于括号常考面试题的主要内容,如果未能解决你的问题,请参考以下文章