2024/9/15 LeetCode Hot100 昨日leetcode,栈第一题
This commit is contained in:
parent
ad3e07dc61
commit
94ad2b4248
|
@ -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("()");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue