Skip to content

Create ValidParentheses.md#6

Open
kt-from-j wants to merge 1 commit intomainfrom
ValidParentheses
Open

Create ValidParentheses.md#6
kt-from-j wants to merge 1 commit intomainfrom
ValidParentheses

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
20. Valid Parentheses
次に解く問題:
206. Reverse Linked List

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.

全体的に読みやすく違和感や引っかかりを感じる部分もありませんでした!
(コード参考にしていただきありがとうございます)

public boolean isValid(String s) {
// stackとして使用する
Deque<Character> stack = new ArrayDeque<>();
Set<Character> openBrackets = new HashSet<>();
Copy link

Choose a reason for hiding this comment

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

openBrackets は初期化後に変更しないため、 Set.of() を使ってシンプルに書けると思います。

Set<Character> openBrackets = Set.of('(', '[', '{');

https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Set.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.

ありがとうございます。静的なSet/Mapはあまり使ったことがなく、この書き方が見えていませんでした。
各リファレンスを読みます。

openBrackets.add('[');
openBrackets.add('{');

for (Character character : s.toCharArray()) {
Copy link

Choose a reason for hiding this comment

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

for (char character : s.toCharArray()) {

と値型で受けて、以下の if 文の条件式を character == ')' と == 演算子で書いたほうが、読みやすいと思います。

ボクシング・アンボクシングによってパフォーマンスが変わるかもしれませんが、パフォーマンスにシビアな場面以外ではあまり気にしなくてよいと思います。パフォーマンスにシビアな場面では、マイクロベンチマークを取り、早い書き方を選ぶとよいと思います。ただ、パフォーマンスにシビアな場面では、 C++ 等、より速い言語を使用したほうが良いかもしれません。

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 (openBrackets.contains(character)) {
stack.push(character);
} else if (character.equals(')')) {
if (Objects.equals(stack.peek(), '(')) {
Copy link

Choose a reason for hiding this comment

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

if (stack.peek().charValue() == '(') {

のほうが読みやすいかしれません。この辺りはチームの平均的な書き方に合わせることをおすすめします。

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 (stack.peek().charValue() == '(') {

例示いただいたコードでボクシングに任せず明示的にcharValue()しているのは、char同士の比較であることを明示するためでしょうか?
私が書くならボクシングに任せて

if (stack.peek() == '(') {

としますが==で書く場合はcharValue()で明示するのが一般的でしょうか?

Copy link

Choose a reason for hiding this comment

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

Java の仕様を勘違いしておりました。この場合はアンボクシングされるので

if (stack.peek() == '(') {

と書くのが良いと思います。
https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21

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として使用する。開き括弧を積む
Deque<Character> stack = new ArrayDeque<>();
// Map<開き括弧, 閉じ括弧>
Map<Character, Character> brackets = new HashMap<>();
Copy link

Choose a reason for hiding this comment

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

こちらも Map.of() を使ったほうがシンプルに書けると思います。

Map<Character, Character> brackets = Map.of('(', '), '[', ']', '{', '}');

https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Map.html

// 開き括弧の場合
if (openToClose.containsKey(bracket)) {
openBracketsStack.push(bracket);
continue;
Copy link

Choose a reason for hiding this comment

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

If節に入った場合、else節はスキップされるので、continue;は不要なように思いました

Copy link

Choose a reason for hiding this comment

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

どちらかというと else の方を消したいですね。
continue がないと続きを探して、else の閉じ括弧を探しに行く必要があります。

Copy link

@26ryss 26ryss Sep 23, 2025

Choose a reason for hiding this comment

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

continue がないと続きを探して、else の閉じ括弧を探しに行く必要があります。

なるほど、そのような意図だったのですね、ありがとうございます。

いずれにせよおっしゃる通り、continueを使う場合は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.

コメントありがとうございます!
確かにこのelseは余計でした。
step3の方では気づいてelseを消してますね。

@5103246
Copy link

5103246 commented Sep 23, 2025

全体的に読みやすかったです!

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.

6 participants