Skip to content

Hashmap 560#16

Open
hiroki-horiguchi-dev wants to merge 2 commits into
mainfrom
hashmap-560
Open

Hashmap 560#16
hiroki-horiguchi-dev wants to merge 2 commits into
mainfrom
hashmap-560

Conversation

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner

@hiroki-horiguchi-dev hiroki-horiguchi-dev commented May 17, 2026

問題を解きました、レビューお願いいたします。
560. Subarray Sum Equals K

Comment thread hashmap/560.md

Map<Integer, Integer> prefixCount = new HashMap<>();
prefixCount.put(0, 1);
int prefixNum = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は prefixSum の方が実態を表す変数名になると思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。おっしゃる通りだと思います。

Comment thread hashmap/560.md
prefixNum += num;
int target = prefixNum - k;
if (prefixCount.containsKey(target)) {
result += prefixCount.get(target);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

getOrDefault を使えば1行になります
https://docs.oracle.com/javase/jp/8/docs/api/java/util/Map.html

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど、確かにそうですね。getOrDefault() で1行なら if で明示的に書くより、私もいいと思います。
取り込みたいと思います。

Comment thread hashmap/560.md
if (prefixCount.containsKey(target)) {
result += prefixCount.get(target);
}
prefixCount.put(prefixNum, prefixCount.getOrDefault(prefixNum, 0) + 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

prefixCount.merge(prefixNum, 1, Integer::sum);

とも書けるみたいです

Copy link
Copy Markdown
Owner Author

@hiroki-horiguchi-dev hiroki-horiguchi-dev May 19, 2026

Choose a reason for hiding this comment

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

ありがとうございます、claudeがレビューで出してきました。
このmerge関数ですが、初見で何しているかよくわからないなあと思ったので、採用見送りました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Goは map[key]++ と書けるのですが、Javaはマップ要素のインクリメントを簡潔に実行する方法なさそうですね

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Go 便利ですね。
hashmap 関係の問題は getOrDefault() を毎回書いてました、少し冗長に感じますよね

@nodchip
Copy link
Copy Markdown

nodchip commented May 18, 2026

PR タイトルに問題名を入れていただけると、読み手にとってレビューしやすくなると思います。

Comment thread hashmap/560.md
```java
class Solution {
public int subarraySum(int[] nums, int k) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メソッドの宣言の次の行に空行を空けるのは、あまり見ないように思います。

Copy link
Copy Markdown
Owner Author

@hiroki-horiguchi-dev hiroki-horiguchi-dev May 19, 2026

Choose a reason for hiding this comment

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

ありがとうございます。
そうですね、書き直した時に空いていたようです。修正します。

Comment thread hashmap/560.md
int prefixNum = 0;
int result = 0;

for(int num : nums) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if for のあとにスペースが空いているものと空いていないものとで混在しているのが気になりました。

参考までにスタイルガイドへのリンクを共有いたします。

https://google.github.io/styleguide/javaguide.html#s4.6.2-horizontal-whitespace

Separating any keyword, such as if, for or catch, from an open parenthesis (() that follows it on that line

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
今まで所属してきたチームでは、if for の前後は空けるでコードを書いてきたので修正します。

Comment thread hashmap/560.md

for(int num : nums) {
prefixNum += num;
int target = prefixNum - k;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

complement(補完値、ついになる値)と名付けている方もいらっしゃいました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
claudeも同様の指摘をしてきたのですが、complementは今回私はあまりしっくりこなかった記憶があります。

Comment thread hashmap/560.md
- 問題: [560. Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/description/)
- コメント集: [](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.e5dwa7yj3tv0)
- [しっくりこない](https://discord.com/channels/1084280443945353267/1233603535862628432/1252232545056063548)
- 私もしっくりこなかったのでちょっと寝かせてしまった。
Copy link
Copy Markdown

@h-masder h-masder May 19, 2026

Choose a reason for hiding this comment

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

わたしもはじめよくわからなかったので、図にしてみました。
https://github.com/h-masder/Arai60/pull/17/changes の560_Subarray_Sum_Equals_K/subarraySum_logic.pngです

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
金銭で例えるのはより身近でわかりやすいと思いました。
自分が納得できる例に問題を言い換えるのも、問題を解く上で重要ですね。

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.

5 participants