From 94ad2b4248d0745869f93b53119461d6eff27385 Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Mon, 16 Sep 2024 13:42:12 +0800 Subject: [PATCH] =?UTF-8?q?2024/9/15=20LeetCode=20Hot100=20=E6=98=A8?= =?UTF-8?q?=E6=97=A5leetcode=EF=BC=8C=E6=A0=88=E7=AC=AC=E4=B8=80=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cool/hot100/stack/Num20.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/com/cool/hot100/stack/Num20.java diff --git a/src/main/java/com/cool/hot100/stack/Num20.java b/src/main/java/com/cool/hot100/stack/Num20.java new file mode 100644 index 0000000..88c535b --- /dev/null +++ b/src/main/java/com/cool/hot100/stack/Num20.java @@ -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 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("()"); + } + +}