2024/9/15 LeetCode Hot100 昨日leetcode,栈第一题

This commit is contained in:
Cool 2024-09-16 13:42:12 +08:00
parent ad3e07dc61
commit 94ad2b4248
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package com.cool.hot100.stack;
import org.junit.Test;
import java.util.*;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/16/2:57
* DayNumber 3
* Hard 1
* Level 3
*/
public class Num20 {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
char[] chars = s.toCharArray();
for (char aChar : chars) {
if(aChar=='('){
stack.push(')');
}else if(aChar=='{'){
stack.push('}');
}else if(aChar=='['){
stack.push(']');
}else if(stack.isEmpty()||stack.pop()!=aChar){
return false;
}
}
return stack.isEmpty();
}
@Test
public void test() {
isValid("()");
}
}