Skip to content

20. Valid Parentheses#7

Merged
yamashita-ki merged 3 commits intomainfrom
leetcode/20
Sep 15, 2025
Merged

20. Valid Parentheses#7
yamashita-ki merged 3 commits intomainfrom
leetcode/20

Conversation

@yamashita-ki
Copy link
Owner

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

お疲れ様です、Javaはあまり詳しくないですが全体的に読みやすく自然なコードだと思いました。分かりやすかったです。
この問題については再帰下降構文解析という解き方もあるのでそちらも見ておくと勉強になるかもしれません
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.5ynll0rwu02h

```java
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javaは詳しくないのですが、過去に講師の方の以下のようなコメントがあったのでArrayDequeを使ったほうがよいのかもしれません

スタックデータ構造であれば、 Stack より ArrayDeque を優先的に使うことをおすすめします。
kt-from-j/leetcode#4 (comment)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少し気になってそれぞれのpop処理を軽く見たのですが、
ArrayDequeで内部で呼ばれるpollFirst()は先頭の値を取り出した後にheadのindexを前に進めるだけなのに対して、Stackで内部で呼ばれるVectorのremoveElementAt()では値をnullにするなどより複雑な処理をしているようです。

https://github.com/openjdk/jdk/blob/5271448b3a013b2e3edcd619a4a3b975b292dae1/src/java.base/share/classes/java/util/ArrayDeque.java#L374-L383

https://github.com/openjdk/jdk/blob/5271448b3a013b2e3edcd619a4a3b975b292dae1/src/java.base/share/classes/java/util/Vector.java#L547-L562

Copy link
Owner Author

@yamashita-ki yamashita-ki Sep 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

お二人とも、ご確認ありがとうございますmm
Javaについて調べていただき助かりますmm

Javaは詳しくないのですが、過去に講師の方の以下のようなコメントがあったのでArrayDequeを使ったほうがよいのかもしれません

ご指摘非常に助かります! 🙇
確かに公式ドキュメントにも記載がありました
今後はStackではなくArrayDequeを使います。

このクラスは通常、スタックとして使われるときはStackよりも高速で、キューとして使われるときはLinkedListよりも高速です。
https://docs.oracle.com/javase/jp/17/docs/api/java.base/java/util/ArrayDeque.html

ArrayDequeで内部で呼ばれるpollFirst()は先頭の値を取り出した後にheadのindexを前に進めるだけなのに対して、Stackで内部で呼ばれるVectorのremoveElementAt()では値をnullにするなどより複雑な処理をしているようです。

ソースコードまで見ていただきありがとうございます! 🙇
ご指摘のとおりVectorでは public synchronized void removeElementAt(int index) とスレッドセーフにするためにsynchronizedを使ってロックを取得しスレッド間で同期をとっているため、スレッドセーフでない(ロックを取らない)ArrayDequeに比べてパフォーマンスが落ちると理解できました。

public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
Map<Character, Character> closeToOpen = new HashMap<>();
closeToOpen.put(']', '[');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

閉じ括弧→開き括弧は普段と順序が逆になっており、ぱっと見で対応が取れているか分かりづらい面もあるかと思います。
その1つの解決策としてopenToCloseにして、stackからpopした開き括弧をopenToCloseに今見ている閉じ括弧と対応しているかを見る方法もあるかなと思いました。

closeToOpen.put(')', '(');
for (Character c : s.toCharArray()) {
if (closeToOpen.containsKey(c)) {
if (!stack.isEmpty() && stack.peek() == closeToOpen.get(c)){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで直接stack.pop()してその値を比較して違ったらreturn Falseを返すとするとelse分岐が要らなくなりそうです

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございますmm
この発想はなかったです!

Copy link

@akmhmgc akmhmgc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他の方がコメントしている部分以外は気になるところはなかったです。

```java
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少し気になってそれぞれのpop処理を軽く見たのですが、
ArrayDequeで内部で呼ばれるpollFirst()は先頭の値を取り出した後にheadのindexを前に進めるだけなのに対して、Stackで内部で呼ばれるVectorのremoveElementAt()では値をnullにするなどより複雑な処理をしているようです。

https://github.com/openjdk/jdk/blob/5271448b3a013b2e3edcd619a4a3b975b292dae1/src/java.base/share/classes/java/util/ArrayDeque.java#L374-L383

https://github.com/openjdk/jdk/blob/5271448b3a013b2e3edcd619a4a3b975b292dae1/src/java.base/share/classes/java/util/Vector.java#L547-L562

@yamashita-ki yamashita-ki merged commit e00793a into main Sep 15, 2025
@5103246
Copy link

5103246 commented Sep 16, 2025

講師陣はこの問題を見るとプッシュダウンオートマトンという単語を想起するみたいです。
余裕があったら調べることをお勧めします。
https://discord.com/channels/1084280443945353267/1206101582861697046/1216945010189144085

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants