括号匹配~~~栈
给定一个字符串,括号必须以正确的方式闭合,即满足如下条件:
- 有效字符串满足:左括号必须同相同类型的右括号闭合:“{}”,"[]","()","<>"
- 左括号必须以正确的顺序闭合
- 空字符串可被认为是有效字符串
思路:
(1)创建一个栈,利用HashMap根据键值对,进行括号匹配
(2)创建栈,根据ASCII不同匹配进行消除
(3)可以利用String类的replaceAll实现【这里不再实现】
两种方式实现:
方式一【利用HashMap实现】:
核心:使用HashMap的键值对来完成匹配
private static void solution01(){
String str = "{()[[()]]<>{}()<>}()";
HashMap<Character,Character> map = new HashMap<>();
//利用HashMap的键值配对来实现
map.put('[',']');
map.put('<','>');
map.put('(',')');
map.put('{','}');
ArrayStack<Character> stack = new ArrayStack<>();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(stack.isEmpty()){
//栈为空,直接进
stack.push(c);
}else{
char top = str.charAt(i);
if(map.containsKey(top) && c == map.get(top)){
//必须提前判断map中是否存在top,因为如果不存在返回的null,不能和c比较
stack.pop();//匹配成功,出栈
}else{
stack.push(c);//匹配失败,进栈
}
}
}
System.out.println(stack.isEmpty());//最后只需要看栈是否为空即可
}
方式二【利用ASCII差值实现】:
private static void solution02(){
String str = "{()[[()]]<>{}()<>}()";
ArrayStack<Character> stack = new ArrayStack<Character>();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(stack.isEmpty()){
//栈为空,不用匹配,直接进
stack.push(c);
}else{
if(stack.peek() - str.charAt(i) == -1 || stack.peek() - str.charAt(i) == -2){
stack.pop();
}else{
stack.push(c);
}
}
System.out.println(stack.isEmpty());
}
全代码:
import java.util.HashMap;
//括号匹配
public class MatchBracket {
public static void main(String[] args) {
solution01();//利用HashMap实现
solution02();
}
private static void solution01() {
//利用HashMap
String str = "{()[[()]]<>{}()<>}()";
HashMap<Character,Character> map = new HashMap<>();
map.put('[',']');
map.put('<','>');
map.put('(',')');
map.put('{','}');
ArrayStack<Character> stack = new ArrayStack<>();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(stack.isEmpty()){
stack.push(c);
}else{
char top = stack.peek();
if(map.containsKey(top) && c == map.get(top)){
stack.pop();
}else{
stack.push(c);
}
}
}
System.out.println(stack.isEmpty());
}
private static void solution02() {
String str = "{()[[()]]<>{}()<>}()";
//利用ASCII
ArrayStack<Character> stack = new ArrayStack<Character>();
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(stack.isEmpty()){
stack.push(c);
}else {
if(stack.peek() - str.charAt(i) == -1 || stack.peek() - str.charAt(i) == -2){
stack.pop();
}else{
stack.push(c);
}
}
}
System.out.println(stack.isEmpty());
}
}
ArrayStack代码实现:
import p1.接口.Stack;
import java.util.Iterator;
public class ArrayStack<E> implements Stack<E> {
private ArrayList<E> list;
public ArrayStack(){
list = new ArrayList<>();
}
public ArrayStack(int capacity){
list = new ArrayList<>(capacity);
}
@Override
public int size() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public void push(E element) {
list.add(element);
}
@Override
public E pop() {
return list.remove(list.size() - 1);
}
@Override
public E peek() {
return list.get(list.size() - 1);
}
@Override
public void clear() {
list.clear();
}
@Override
public Iterator iterator() {
return list.iterator();
}
@Override
public String toString() {
return list.toString();
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (this == o) {
return true;
}
if (o instanceof ArrayStack) {
ArrayStack other = (ArrayStack) o;
return this.list.equals(other.list);
}
return false;
}
}