解决思路:
1. 栈
2.使用Map,判断是否
public boolean isValid(String s) { Stack<Character> parens = new Stack<>(); Map<Character, Character> parenMap = new HashMap<>(); parenMap.put(‘(‘, ‘)‘); parenMap.put(‘[‘, ‘]‘); parenMap.put(‘{‘, ‘}‘); for (int i = 0; i < s.length(); i++) { char paren = s.charAt(i); boolean isLeft = parenMap.containsKey(paren); if (isLeft) { parens.push(paren); } else { if (parens.empty()) { return false; } char leftSide = parens.pop(); if (parenMap.get(leftSide) != paren) { return false; } } } return parens.empty(); }
匹配
原文地址:https://www.cnblogs.com/da-peng/p/8278050.html
时间: 2024-11-10 14:08:21