使用堆栈算法进行括号/括号匹配
Posted
技术标签:
【中文标题】使用堆栈算法进行括号/括号匹配【英文标题】:Parenthesis/Brackets Matching using Stack algorithm 【发布时间】:2015-06-06 10:27:21 【问题描述】:例如,如果括号/括号在以下匹配:
()
(())()
()
以此类推,但如果括号/方括号不匹配,则应返回 false,例如:
((
))
(()
等等。你能检查一下这个代码吗?提前致谢。
public static boolean isParenthesisMatch(String str)
Stack<Character> stack = new Stack<Character>();
char c;
for(int i=0; i < str.length(); i++)
c = str.charAt(i);
if(c == '')
return false;
if(c == '(')
stack.push(c);
if(c == '')
stack.push(c);
if(c == '')
if(stack.empty())
return false;
else if(stack.peek() == '')
stack.pop();
else if(c == ')')
if(stack.empty())
return false;
else if(stack.peek() == '(')
stack.pop();
else
return false;
return stack.empty();
public static void main(String[] args)
String str = "()";
System.out.println(Weekly12.parenthesisOtherMatching(str));
【问题讨论】:
你说“parens”,但你似乎也想检查括号...... 这里的java有什么特别之处? 问题。[ ]
是否有效匹配?
如果没有语言规范(即“根据哪些规则是您要解析的表达式”的问题的精确答案),我们无法回答这个问题。它们是上下文无关的语言吗?仅由于括号,它们绝对不是常规的。它们是上下文相关的吗?图灵完备?不管怎样,这个问题应该在 CS.SE 上
或者你可以编写一个真正的解析器而不是滥用正则表达式。
【参考方案1】:
您的代码在处理“”和“”字符时有些混乱。它应该与您处理 '(' 和 ')' 的方式完全平行。
这段代码稍加修改,似乎可以正常工作:
public static boolean isParenthesisMatch(String str)
if (str.charAt(0) == '')
return false;
Stack<Character> stack = new Stack<Character>();
char c;
for(int i=0; i < str.length(); i++)
c = str.charAt(i);
if(c == '(')
stack.push(c);
else if(c == '')
stack.push(c);
else if(c == ')')
if(stack.empty())
return false;
else if(stack.peek() == '(')
stack.pop();
else
return false;
else if(c == '')
if(stack.empty())
return false;
else if(stack.peek() == '')
stack.pop();
else
return false;
return stack.empty();
【讨论】:
谢谢,但问题是 , 甚至 (), () 它应该返回 false。换句话说,第一个 c == 应该是 false。 它适用于整个文件还是仅适用于单行?假设(
在文件的第一行,但)
在文件的第二行。这种情况可以查吗?【参考方案2】:
这段代码比较容易理解:
public static boolean CheckParentesis(String str)
if (str.isEmpty())
return true;
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < str.length(); i++)
char current = str.charAt(i);
if (current == '' || current == '(' || current == '[')
stack.push(current);
if (current == '' || current == ')' || current == ']')
if (stack.isEmpty())
return false;
char last = stack.peek();
if (current == '' && last == '' || current == ')' && last == '(' || current == ']' && last == '[')
stack.pop();
else
return false;
return stack.isEmpty();
【讨论】:
很棒的代码,虽然可以通过在将当前字符压入堆栈后添加一条 continue 语句来稍微改进它。【参考方案3】:public static boolean isValidExpression(String expression)
Map<Character, Character> openClosePair = new HashMap<Character, Character>();
openClosePair.put(')', '(');
openClosePair.put('', '');
openClosePair.put(']', '[');
Stack<Character> stack = new Stack<Character>();
for(char ch : expression.toCharArray())
if(openClosePair.containsKey(ch))
if(stack.pop() != openClosePair.get(ch))
return false;
else if(openClosePair.values().contains(ch))
stack.push(ch);
return stack.isEmpty();
【讨论】:
为什么 OP 应该使用你的代码?你能用解释来扩展你的答案吗? 发现非法关闭后立即返回结果。使用实习生使用散列技术的 Map 并且速度更快。行数较少,易于理解。openClosePair.values().contains(ch)
被替换openClosePair.containsValue(ch)
你的最后一对是向后更改为 (']','[')
并且需要检查栈是否为空 if(stack.empty() || stack.pop() != openClosedPair.get(ch)) return false; 【参考方案4】:
算法:
-
扫描字符串,将字符串中的每个 '(' 推入堆栈
如果 char ')' 被扫描,则从堆栈中弹出一个 '('
现在,括号在两个条件下是平衡的:
'(' 可以从堆栈中弹出字符串中的每个 ')',并且 堆栈最后为空(处理整个字符串时)【讨论】:
即使我在该算法中添加 '' 和 '' 条件,它也不适用于 -()
。我们必须检查在每个打开的LAST
括号/括号之后,SAME
是否必须关闭。【参考方案5】:
实际上,没有必要“手动”检查任何案例。您可以运行以下算法:
遍历给定的序列。从空堆栈开始。
如果当前字符是左括号,只需将其压入堆栈即可。
1234563如果不是,则报告错误。否则,从堆栈中弹出顶部元素。最后,如果堆栈为空,则顺序正确。
为什么是正确的?这是一个证明的草图:如果该算法报告该序列已被纠正,则它已找到所有括号的匹配对。因此,根据定义,该序列确实是正确的。如果报错:
如果栈最后不为空,则开闭括号的余额不为零。因此,这不是一个正确的顺序。
如果当我们不得不弹出一个元素时堆栈是空的,那么平衡就会再次关闭。
如果堆栈顶部有错误的元素,一对“错误”的括号应该相互匹配。表示顺序不正确。
我已经证明:
如果算法已经报告序列是正确的,那就是正确的。
如果算法报告了序列不正确,那就是不正确(请注意,我没有使用除了您的问题中提到的情况之外没有其他情况的事实)。
这两点表明该算法适用于所有可能的输入。
【讨论】:
问题不仅仅在于正确嵌套括号。 注意:您的答案已从 ***.com/questions/29396477/… 合并到此处 - 请根据需要进行调整。 上述算法在javascript中的实现可以在这里找到(gist.github.com/sanketmaru/e83ce04100966bf46f6e8919a06c33ba)。可以测试所有可能的输入。【参考方案6】:public static boolean isBalanced(String s)
Map<Character, Character> openClosePair = new HashMap<Character, Character>();
openClosePair.put('(', ')');
openClosePair.put('', '');
openClosePair.put('[', ']');
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++)
if (openClosePair.containsKey(s.charAt(i)))
stack.push(s.charAt(i));
else if ( openClosePair.containsValue(s.charAt(i)))
if (stack.isEmpty())
return false;
if (openClosePair.get(stack.pop()) != s.charAt(i))
return false;
// ignore all other characters
return stack.isEmpty();
【讨论】:
【参考方案7】:Ganesan 上面的回答不正确,*** 不允许我评论或编辑他的帖子。所以下面是正确答案。 Ganesan 有一个不正确的“[”,并且缺少堆栈 isEmpty() 检查。
如果大括号正确匹配,下面的代码将返回 true。
public static boolean isValidExpression(String expression)
Map<Character, Character> openClosePair = new HashMap<Character, Character>();
openClosePair.put(')', '(');
openClosePair.put('', '');
openClosePair.put(']', '[');
Stack<Character> stack = new Stack<Character>();
for(char ch : expression.toCharArray())
if(openClosePair.containsKey(ch))
if(stack.isEmpty() || stack.pop() != openClosePair.get(ch))
return false;
else if(openClosePair.values().contains(ch))
stack.push(ch);
return stack.isEmpty();
【讨论】:
【参考方案8】:public boolean isValid(String s)
Map<Character, Character> map = new HashMap<>();
map.put('(', ')');
map.put('[', ']');
map.put('', '');
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray())
if(map.containsKey(c))
stack.push(c);
else if(!stack.empty() && map.get(stack.peek())==c)
stack.pop();
else
return false;
return stack.empty();
【讨论】:
【参考方案9】:算法是:
1)Create a stack
2)while(end of input is not reached)
i)if the character read is not a sysmbol to be balanced ,ignore it.
ii)if the character is ,[,( then push it to stack
iii)If it is a ,),] then if
a)the stack is empty report an error(catch it) i.e not balanced
b)else pop the stack
iv)if element popped is not corresponding to opening sysmbol,then report error.
3) In the end,if stack is not empty report error else expression is balanced.
在Java代码中:
public class StackDemo
public static void main(String[] args) throws Exception
System.out.println("--Bracket checker--");
CharStackArray stack = new CharStackArray(10);
stack.balanceSymbol("[a+bc+(e-f[p-q])]") ;
stack.display();
class CharStackArray
private char[] array;
private int top;
private int capacity;
public CharStackArray(int cap)
capacity = cap;
array = new char[capacity];
top = -1;
public void push(char data)
array[++top] = data;
public char pop()
return array[top--];
public void display()
for (int i = 0; i <= top; i++)
System.out.print(array[i] + "->");
public char peek() throws Exception
return array[top];
/*Call this method by passing a string expression*/
public void balanceSymbol(String str)
try
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++)
if (arr[i] == '[' || arr[i] == '' || arr[i] == '(')
push(arr[i]);
else if (arr[i] == '' && peek() == '')
pop();
else if (arr[i] == ']' && peek() == '[')
pop();
else if (arr[i] == ')' && peek() == '(')
pop();
if (isEmpty())
System.out.println("String is balanced");
else
System.out.println("String is not balanced");
catch (Exception e)
System.out.println("String not balanced");
public boolean isEmpty()
return (top == -1);
输出:
--括号检查器--
弦是平衡的
【讨论】:
【参考方案10】:使用 Stacks 和 Switch 语句优化实现:
public class JavaStack
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
Stack<Character> s = new Stack<Character>();
while (sc.hasNext())
String input = sc.next();
for (int i = 0; i < input.length(); i++)
char c = input.charAt(i);
switch (c)
case '(':
s.push(c); break;
case '[':
s.push(c); break;
case '':
s.push(c); break;
case ')':
if (!s.isEmpty() && s.peek().equals('('))
s.pop();
else
s.push(c);
break;
case ']':
if (!s.isEmpty() && s.peek().equals('['))
s.pop();
else
s.push(c);
break;
case '':
if (!s.isEmpty() && s.peek().equals(''))
s.pop();
else
s.push(c);
break;
default:
s.push('x'); break;
if (s.empty())
System.out.println("true");
else
System.out.println("false");
s.clear();
干杯!
【讨论】:
【参考方案11】:您正在执行一些不需要的额外检查。不会对功能造成任何差异,但编写代码的更简洁的方法是:
public static boolean isParenthesisMatch(String str)
Stack<Character> stack = new Stack<Character>();
char c;
for (int i = 0; i < str.length(); i++)
c = str.charAt(i);
if (c == '(' || c == '')
stack.push(c);
else if (stack.empty())
return false;
else if (c == ')')
if (stack.pop() != '(')
return false;
else if (c == '')
if (stack.pop() != '')
return false;
return stack.empty();
没有理由在将括号从堆栈中删除之前先查看它。我还考虑将指令块包装在括号中以提高可读性。
【讨论】:
【参考方案12】:import java.util.*;
class StackDemo
public static void main(String[] argh)
boolean flag = true;
String str = "(())()";
int l = str.length();
flag = true;
Stack<String> st = new Stack<String>();
for (int i = 0; i < l; i++)
String test = str.substring(i, i + 1);
if (test.equals("("))
st.push(test);
else if (test.equals(""))
st.push(test);
else if (test.equals("["))
st.push(test);
else if (test.equals(")"))
if (st.empty())
flag = false;
break;
if (st.peek().equals("("))
st.pop();
else
flag = false;
break;
else if (test.equals(""))
if (st.empty())
flag = false;
break;
if (st.peek().equals(""))
st.pop();
else
flag = false;
break;
else if (test.equals("]"))
if (st.empty())
flag = false;
break;
if (st.peek().equals("["))
st.pop();
else
flag = false;
break;
if (flag && st.empty())
System.out.println("true");
else
System.out.println("false");
【讨论】:
虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。【参考方案13】:我在这里看到了答案,几乎所有答案都做得很好。但是,我编写了自己的版本,它利用字典来管理括号对和堆栈来监视检测到的括号的顺序。我也为此写了一篇博客post。
这是我的课
public class FormulaValidator
// Question: Check if a string is balanced. Every opening bracket is matched by a closing bracket in a correct position.
// [ ( ] )
// Example: "()" is balanced
// Example: " ]" is not balanced.
// Examples: "()[]" is balanced.
// "([])" is balanced
// " ( [ ) ] " is _not_ balanced
// Input: string, containing the bracket symbols only
// Output: true or false
public bool IsBalanced(string input)
var brackets = BuildBracketMap();
var openingBraces = new Stack<char>();
var inputCharacters = input.ToCharArray();
foreach (char character in inputCharacters)
if (brackets.ContainsKey(character))
openingBraces.Push(character);
if (brackets.ContainsValue(character))
var closingBracket = character;
var openingBracket = brackets.FirstOrDefault(x => x.Value == closingBracket).Key;
if (openingBraces.Peek() == openingBracket)
openingBraces.Pop();
else
return false;
return openingBraces.Count == 0;
private Dictionary<char, char> BuildBracketMap()
return new Dictionary<char, char>()
'[', ']',
'(', ')',
'', ''
;
【讨论】:
【参考方案14】:用于检查括号是否平衡的算法 -
-
声明一个映射matchingParenMap,并分别用每种类型的右括号和左括号作为键值对来初始化它。
声明一个集合openingParenSet并用matchingParenMap的值初始化它。
声明一个堆栈parenStack,它将存储左括号''、'('和'['。
现在遍历字符串表达式input。
如果当前字符是一个左括号('', '(', '[') 则按它到 parenStack。
如果当前字符是右括号('', ')', ']')然后弹出从 parenStack 并且如果弹出的字符等于匹配的起始括号 matchingParenMap 然后继续循环,否则返回 false。
完全遍历后,如果parenStack中没有左括号,则表示它是一个平衡良好的表达式。
我已经解释了我博客上使用的算法的代码 sn-p。检查链接 - http://hetalrachh.home.blog/2019/12/25/stack-data-structure/
【讨论】:
【参考方案15】:问题陈述: 检查表达式中的平衡括号或匹配左括号
如果您参加编码面试,那么您之前可能遇到过这个问题。这是一个很常见的问题,可以通过使用 Stack Data Structure 来解决 C#中的解决方案
public void OpenClosingBracketsMatch()
string pattern = "[(((((])";
Dictionary<char, char> matchLookup = new Dictionary<char, char>();
matchLookup[''] = '';
matchLookup['('] = ')';
matchLookup['['] = ']';
Stack<char> stck = new Stack<char>();
for (int i = 0; i < pattern.Length; i++)
char currentChar = pattern[i];
if (matchLookup.ContainsKey(currentChar))
stck.Push(currentChar);
else if (currentChar == '' || currentChar == ')' || currentChar == ']')
char topCharFromStack = stck.Peek();
if (matchLookup[topCharFromStack] != currentChar)
Console.WriteLine("NOT Matched");
return;
Console.WriteLine("Matched");
更多信息,您也可以参考这个链接:https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/
【讨论】:
您好!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释并说明适用的限制和假设。 感谢@Brian 指出这一点!我在帖子中添加了更多描述【参考方案16】:这是我使用 C++ 的解决方案 如果括号匹配则返回 true 如果不匹配则返回 false
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int matchbracket(string expr)
stack<char> st;
int i;
char x;
for(i=0;i<expr.length();i++)
if(expr[i]=='('||expr[i]==''||expr[i]=='[')
st.push(expr[i]);
if(st.empty())
return -1;
switch(expr[i])
case ')' :
x=expr[i];
st.pop();
if(x==''||x==']')
return 0;
break;
case '' :
x=expr[i];
st.pop();
if(x==')'||x==']')
return 0;
break;
case ']' :
x=expr[i];
st.pop();
if(x==')'||x=='')
return 1;
break;
return(st.empty());
int main()
string expr;
cin>>expr;
if(matchbracket(expr)==1)
cout<<"\nTRUE\n";
else
cout<<"\nFALSE\n";
【讨论】:
【参考方案17】://basic code non strack algorithm just started learning java ignore space and time.
/// [()][][]
// [( -a -> ]) -b -> replace a(]) -> reverse a( ]))->
//Split string to substring [()], next [], next [], next
public class testbrackets
static String stringfirst;
static String stringsecond;
static int open = 0;
public static void main(String[] args)
splitstring("(())()");
static void splitstring(String str)
int len = str.length();
for(int i=0;i<=len-1;i++)
stringfirst="";
stringsecond="";
System.out.println("loop starttttttt");
char a = str.charAt(i);
if(a==''||a=='['||a=='(')
open = open+1;
continue;
if(a==''||a==']'||a==')')
if(open==0)
System.out.println(open+"started with closing brace");
return;
String stringfirst=str.substring(i-open, i);
System.out.println("stringfirst"+stringfirst);
String stringsecond=str.substring(i, i+open);
System.out.println("stringsecond"+stringsecond);
replace(stringfirst, stringsecond);
i=(i+open)-1;
open=0;
System.out.println(i);
static void replace(String stringfirst, String stringsecond)
stringfirst = stringfirst.replace('', '');
stringfirst = stringfirst.replace('(', ')');
stringfirst = stringfirst.replace('[', ']');
StringBuilder stringfirst1 = new StringBuilder(stringfirst);
stringfirst = stringfirst1.reverse().toString();
System.out.println("stringfirst"+stringfirst);
System.out.println("stringsecond"+stringsecond);
if(stringfirst.equals(stringsecond))
System.out.println("pass");
else
System.out.println("fail");
System.exit(0);
【讨论】:
这与 OP 发布的代码完全不同。如果您能稍微解释一下,这对其他人将非常有帮助,以便我们了解您的思路。 另外它太长了。您还应该尽可能避免从方法内部打印。【参考方案18】:import java.util.Stack;
class Demo
char c;
public boolean checkParan(String word)
Stack<Character> sta = new Stack<Character>();
for(int i=0;i<word.length();i++)
c=word.charAt(i);
if(c=='(')
sta.push(c);
System.out.println("( Pushed into the stack");
else if(c=='')
sta.push(c);
System.out.println("( Pushed into the stack");
else if(c==')')
if(sta.empty())
System.out.println("Stack is Empty");
return false;
else if(sta.peek()=='(')
sta.pop();
System.out.println(" ) is poped from the Stack");
else if(sta.peek()=='(' && sta.empty())
System.out.println("Stack is Empty");
return false;
else if(c=='')
if(sta.empty())
System.out.println("Stack is Empty");
return false;
else if(sta.peek()=='')
sta.pop();
System.out.println(" is poped from the Stack");
else if(c=='(')
if(sta.empty())
System.out.println("Stack is empty only ( parenthesis in Stack ");
// System.out.print("The top element is : "+sta.peek());
return sta.empty();
public class ParaenthesisChehck
/**
* @param args the command line arguments
*/
public static void main(String[] args)
// TODO code application logic here
Demo d1= new Demo();
// d1.checkParan(" ");
// d1.checkParan("");
//d1.checkParan("()");
//d1.checkParan("()");
// d1.checkParan("123");
d1.checkParan("");
【讨论】:
【参考方案19】:import java.util.*;
public class Parenthesis
public static void main(String...okok)
Scanner sc= new Scanner(System.in);
String str=sc.next();
System.out.println(isValid(str));
public static int isValid(String a)
if(a.length()%2!=0)
return 0;
else if(a.length()==0)
return 1;
else
char c[]=a.toCharArray();
Stack<Character> stk = new Stack<Character>();
for(int i=0;i<c.length;i++)
if(c[i]=='(' || c[i]=='[' || c[i]=='')
stk.push(c[i]);
else
if(stk.isEmpty())
return 0;
//break;
else
char cc=c[i];
if(cc==')' && stk.peek()=='(' )
stk.pop();
else if(cc==']' && stk.peek()=='[' )
stk.pop();
else if(cc=='' && stk.peek()=='' )
stk.pop();
if(stk.isEmpty())
return 1;
else
return 0;
【讨论】:
【参考方案20】:我在下面使用 javascript 进行了尝试,结果如下。
function bracesChecker(str)
if(!str)
return true;
var openingBraces = ['', '[', '('];
var closingBraces = ['', ']', ')'];
var stack = [];
var openIndex;
var closeIndex;
//check for opening Braces in the val
for (var i = 0, len = str.length; i < len; i++)
openIndex = openingBraces.indexOf(str[i]);
closeIndex = closingBraces.indexOf(str[i]);
if(openIndex !== -1)
stack.push(str[i]);
if(closeIndex !== -1)
if(openingBraces[closeIndex] === stack[stack.length-1])
stack.pop();
else
return false;
if(stack.length === 0)
return true;
else
return false;
var testStrings = [
'',
'test',
'[][]()()()[]()',
'test[test]',
'test[test]',
'test(yo)[test]',
'test[test]',
'te()s[]t[test]',
'te()s[]t[test'
];
testStrings.forEach(val => console.log(`$val => $bracesChecker(val)`));
【讨论】:
【参考方案21】:import java.util.*;
public class MatchBrackets
public static void main(String[] argh)
String input = "[][]()";
System.out.println (input);
char [] openChars = '[','','(';
char [] closeChars = ']','',')';
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < input.length(); i++)
String x = "" +input.charAt(i);
if (String.valueOf(openChars).indexOf(x) != -1)
stack.push(input.charAt(i));
else
Character lastOpener = stack.peek();
int idx1 = String.valueOf(openChars).indexOf(lastOpener.toString());
int idx2 = String.valueOf(closeChars).indexOf(x);
if (idx1 != idx2)
System.out.println("false");
return;
else
stack.pop();
if (stack.size() == 0)
System.out.println("true");
else
System.out.println("false");
【讨论】:
【参考方案22】:如果你想看看我的代码。仅供参考
public class Default
public static void main(String[] args) throws IOException
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numOfString = Integer.parseInt(br.readLine());
String s;
String stringBalanced = "YES";
Stack<Character> exprStack = new Stack<Character>();
while ((s = br.readLine()) != null)
stringBalanced = "YES";
int length = s.length() - 1;
for (int i = 0; i <= length; i++)
char tmp = s.charAt(i);
if(tmp=='[' || tmp=='' || tmp=='(')
exprStack.push(tmp);
else if(tmp==']' || tmp=='' || tmp==')')
if(!exprStack.isEmpty())
char peekElement = exprStack.peek();
exprStack.pop();
if(tmp==']' && peekElement!='[')
stringBalanced="NO";
else if(tmp=='' && peekElement!='')
stringBalanced="NO";
else if(tmp==')' && peekElement!='(')
stringBalanced="NO";
else
stringBalanced="NO";
break;
if(!exprStack.isEmpty())
stringBalanced = "NO";
exprStack.clear();
System.out.println(stringBalanced);
【讨论】:
【参考方案23】:public static bool IsBalanced(string input)
Dictionary<char, char> bracketPairs = new Dictionary<char, char>()
'(', ')' ,
'', '' ,
'[', ']' ,
'<', '>'
;
Stack<char> brackets = new Stack<char>();
try
// Iterate through each character in the input string
foreach (char c in input)
// check if the character is one of the 'opening' brackets
if (bracketPairs.Keys.Contains(c))
// if yes, push to stack
brackets.Push(c);
else
// check if the character is one of the 'closing' brackets
if (bracketPairs.Values.Contains(c))
// check if the closing bracket matches the 'latest' 'opening' bracket
if (c == bracketPairs[brackets.First()])
brackets.Pop();
else
// if not, its an unbalanced string
return false;
else
// continue looking
continue;
catch
// an exception will be caught in case a closing bracket is found,
// before any opening bracket.
// that implies, the string is not balanced. Return false
return false;
// Ensure all brackets are closed
return brackets.Count() == 0 ? true : false;
【讨论】:
【参考方案24】:public String checkString(String value)
Stack<Character> stack = new Stack<>();
char topStackChar = 0;
for (int i = 0; i < value.length(); i++)
if (!stack.isEmpty())
topStackChar = stack.peek();
stack.push(value.charAt(i));
if (!stack.isEmpty() && stack.size() > 1)
if ((topStackChar == '[' && stack.peek() == ']') ||
(topStackChar == '' && stack.peek() == '') ||
(topStackChar == '(' && stack.peek() == ')'))
stack.pop();
stack.pop();
return stack.isEmpty() ? "YES" : "NO";
【讨论】:
【参考方案25】:这里有一个 Python 解决方案。
#!/usr/bin/env python
def brackets_match(brackets):
stack = []
for char in brackets:
if char == "" or char == "(" or char == "[":
stack.append(char)
if char == "":
if stack[-1] == "":
stack.pop()
else:
return False
elif char == "]":
if stack[-1] == "[":
stack.pop()
else:
return False
elif char == ")":
if stack[-1] == "(":
stack.pop()
else:
return False
if len(stack) == 0:
return True
else:
return False
if __name__ == "__main__":
print(brackets_match("This is testing ([]) if brackets have match."))
【讨论】:
【参考方案26】:在现场编码面试中被要求实现此算法,这是我在 C# 中重构的解决方案:
GitTests
【讨论】:
【参考方案27】:package com.balance.braces;
import java.util.Arrays;
import java.util.Stack;
public class BalanceBraces
public static void main(String[] args)
String[] values = "()]", "[()]" ;
String[] rsult = match(values);
Arrays.stream(rsult).forEach(str -> System.out.println(str));
static String[] match(String[] values)
String[] returnString = new String[values.length];
for (int i = 0; i < values.length; i++)
String value = values[i];
if (value.length() % 2 != 0)
returnString[i] = "NO";
continue;
else
Stack<Character> buffer = new Stack<Character>();
for (char ch : value.toCharArray())
if (buffer.isEmpty())
buffer.add(ch);
else
if (isMatchedBrace(buffer.peek(), ch))
buffer.pop();
else
buffer.push(ch);
if (buffer.isEmpty())
returnString[i] = "YES";
else
returnString[i] = "FALSE";
return returnString;
static boolean isMatchedBrace(char start, char endmatch)
if (start == '')
return endmatch == '';
if (start == '(')
return endmatch == ')';
if (start == '[')
return endmatch == ']';
return false;
【讨论】:
【参考方案28】:在 java 中,您不想通过 == 符号比较字符串或字符。你会使用equals方法。 equalsIgnoreCase 或类似的东西。如果你使用 == 它必须指向相同的内存位置。在下面的方法中,我尝试使用整数来解决这个问题。在此处使用字符串索引中的整数,因为每个左大括号都有一个右大括号。我想使用位置匹配而不是比较匹配。但我认为这样你必须有意识地放置字符串的字符。为简单起见,我们还考虑 Yes = true 和 No = false。这个答案假设您传递了一个字符串数组来检查并需要一个数组,如果是(它们匹配)或否(它们不匹配)
import java.util.Stack;
public static void main(String[] args)
//String[] arrayOfBraces = new String[]"[]","([])","()","","]","[)]()";
// Example: "()" is balanced
// Example: " ]" is not balanced.
// Examples: "()[]" is balanced.
// "([])" is balanced
// "([)]" is _not_ balanced
String[] arrayOfBraces = new String[]"[]","([])","()","()[]","]","[)]()","[)]()","([)]";
String[] answers = new String[arrayOfBraces.length];
String openers = "([";
String closers = ")]";
String stringToInspect = "";
Stack<String> stack = new Stack<String>();
for (int i = 0; i < arrayOfBraces.length; i++)
stringToInspect = arrayOfBraces[i];
for (int j = 0; j < stringToInspect.length(); j++)
if(stack.isEmpty())
if (openers.indexOf(stringToInspect.charAt(j))>=0)
stack.push(""+stringToInspect.charAt(j));
else
answers[i]= "NO";
j=stringToInspect.length();
else if(openers.indexOf(stringToInspect.charAt(j))>=0)
stack.push(""+stringToInspect.charAt(j));
else
String comparator = stack.pop();
int compLoc = openers.indexOf(comparator);
int thisLoc = closers.indexOf(stringToInspect.charAt(j));
if (compLoc != thisLoc)
answers[i]= "NO";
j=stringToInspect.length();
else
if(stack.empty() && (j== stringToInspect.length()-1))
answers[i]= "YES";
System.out.println(answers.length);
for (int j = 0; j < answers.length; j++)
System.out.println(answers[j]);
【讨论】:
【参考方案29】: Check balanced parenthesis or brackets with stack--
var excp = "()[a+b+b][(c+d)][]";
var stk = [];
function bracket_balance()
for(var i=0;i<excp.length;i++)
if(excp[i]=='[' || excp[i]=='(' || excp[i]=='')
stk.push(excp[i]);
else if(excp[i]== ']' && stk.pop() != '[')
return false;
else if(excp[i]== '' && stk.pop() != '')
return false;
else if(excp[i]== ')' && stk.pop() != '(')
return false;
return true;
console.log(bracket_balance());
//Parenthesis are balance then return true else false
【讨论】:
【参考方案30】:不使用栈的括号匹配程序
这里我使用字符串替换堆栈实现,如推送和弹出操作。
`package java_prac; 导入 java.util.*; 公共类括号检查器
public static void main(String[] args)
System.out.println("- - - Brackets Checker [ without stack ] - - -\n\n");
Scanner scan=new Scanner(System.in);
System.out.print("Input : " );
String s = scan.nextLine();
scan.close();
System.out.println("\n...working...\n");
String o="([";
String c=")]";
String x=" ";
int check =0;
for (int i = 0; i < s.length(); i++)
if(o.contains(String.valueOf(s.charAt(i))))
x+=s.charAt(i);
//System.out.println("In : "+x); // stack in
else if(c.contains(String.valueOf(s.charAt(i))))
char temp = s.charAt(i);
if(temp==')') temp = '(';
if(temp=='') temp = '';
if(temp==']') temp = '[';
if(x.charAt(x.length()-1)==temp)
x=" "+x.substring(1,x.length()-1);
//System.out.println("Out : "+x); // stack out
else
check=1;
if(x.length()==1 && check==0 )
System.out.println("\n\nCompilation Success \n\n© github.com/sharanstark 2k19");
else
System.out.println("\n\nCompilation Error \n\n© github.com/sharanstark 2k19" );
`
【讨论】:
请始终将您的答案放在上下文中,而不仅仅是粘贴代码。有关详细信息,请参阅here。 请不要只发布代码作为答案,还要说明您的代码的作用以及它如何解决问题。以上是关于使用堆栈算法进行括号/括号匹配的主要内容,如果未能解决你的问题,请参考以下文章