Skip to content

20. Valid Parentheses #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

20. Valid Parentheses #14

wants to merge 4 commits into from

Conversation

katsukii
Copy link
Owner

問題

20. Valid Parentheses

言語

Java

次の問題

276. Paint Fence


```java
class Solution {
public static final Set<Character> OPEN_BRACKETS = Set.of(
Copy link

Choose a reason for hiding this comment

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

他のクラスから参照されない定数は private にしたほうがよいと思います。

Copy link
Owner Author

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) {
Deque<Character> stack = new ArrayDeque<>();
Copy link

Choose a reason for hiding this comment

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

念のため知識面を確認させてください。 LinkedList を使用する場合と比べ、 ArrayDeque はどのような利点がありますか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

正直、stackの実装にはArrayDequeを使うという形で機械的に考えていたので、LinkedListで実装できるという発想すらなかったですね。。

ご質問に対して調べずに回答すると、ArrayDequeの方がLinkedListよりメモリ効率が良いと思います。LinkedListは各要素が隣のノードの参照を持つため1要素のメモリ消費が大きいのではないかと思います。

Copy link

Choose a reason for hiding this comment

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

ありがとうございます。メモリ効率についてはその通りなのですが、 ArrayDeque より高速であるという点のほうが重要だと思います。詳しくは Javadoc をご覧ください。
https://docs.oracle.com/javase/jp/8/docs/api/java/util/ArrayDeque.html
疑問に思ったときに公式ドキュメントを読む癖をつけることをお勧めいたします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

公式ドキュメントに明記してあるのですね。読む癖をつけようと思います。

'(', '[', '{'
);

public static final Map<Character, Character> BRACKET_CLOSE_OPEN = Map.of(
Copy link

@nodchip nodchip Jan 31, 2025

Choose a reason for hiding this comment

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

キーを開きカッコ、値を閉じカッコにした Map のみ持たせる方法もあります。

if (BLACKET_OPEN_CLOSE.containsKey(c)) {
    stack.push(c);
}
else if (stack.isEmpty())
{
    return false;
}
else if (BLACKET_OPEN_CLOSE.get(stack.getLast()) == c)
{
    stack.pop();
}
else
{
    return false;
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、たしかにこの書き方であればいけますね。ありがとうございます。


for (char c : s.toCharArray()) {
if (OPEN_BRACKETS.contains(c)) {
stack.push(c);
Copy link

Choose a reason for hiding this comment

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

私は個人的にはここで continue と書いてしまうのが好みですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。たしかに後続の判定必要ないのでその方がいいですね。

if (OPEN_BRACKETS.contains(c)) {
stack.push(c);
}
if (BRACKET_CLOSE_OPEN.containsKey(c)) {
Copy link

Choose a reason for hiding this comment

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

これは括弧でないものが入ってきたとしたら、その字は何もしないで流すコードになっていますが、意図的でしょうか。異常な入力への対応は、一応念頭に置いてください。何かをする必要がかならずあるわけではないです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.jdtk9v35bca4

個人的には、条件をひっくり返して、continue にして下のインデントを下げます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

いえ、意図できていませんでした。LeetCodeを解く際、流れてくるデータの内容は保証されているという前提で解いてしまっておりました。あるいは、データが変更されたとしたらその時に変更してしまえばいいくらいに考えてた気がします。

あくまで大局観や想像力を養うという目的だと思うので、LeetCodeのルールに囚われず念頭に置いて取り組むようにします。

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.

3 participants