classSolution12{publicbooleancheckValidString(String s){Deque<Integer> left =newLinkedList<>();//保存左括号的坐标Deque<Integer> star =newLinkedList<>();//保存*的坐标for(int i =0; i < s.length(); i++){if(s.charAt(i)=='('){//左括号就让下标入栈
left.offerLast(i);}elseif(s.charAt(i)=='*'){//入栈
star.offerLast(i);}else{//右括号,出栈。当left、star 都为空时,返回falseif(left.isEmpty()){if(star.isEmpty())returnfalse;
star.pollLast();}}}//判断剩余的左括号栈的元素,不为空,说明还有左括号,将*当做右括号,但*的下标必须在左括号的右边才符合条件if(!left.isEmpty()){while(!left.isEmpty()&&!star.isEmpty()){if(left.pollLast()> star.pollLast())returnfalse;}}//判断左括号栈是否为空,为空,*都可以当做空字符符合条件,返回true 。return left.isEmpty();}}
有效的括号字符串 LeetCode678原题链接class Solution12 { public boolean checkValidString(String s) { Deque<Integer> left = new LinkedList<>();//保存左括号的坐标 Deque<Integer> star = new LinkedList<>();//保存*的坐标 fo