Hashmap 560#16
Conversation
|
|
||
| Map<Integer, Integer> prefixCount = new HashMap<>(); | ||
| prefixCount.put(0, 1); | ||
| int prefixNum = 0; |
There was a problem hiding this comment.
ありがとうございます。おっしゃる通りだと思います。
| prefixNum += num; | ||
| int target = prefixNum - k; | ||
| if (prefixCount.containsKey(target)) { | ||
| result += prefixCount.get(target); |
There was a problem hiding this comment.
getOrDefault を使えば1行になります
https://docs.oracle.com/javase/jp/8/docs/api/java/util/Map.html
There was a problem hiding this comment.
なるほど、確かにそうですね。getOrDefault() で1行なら if で明示的に書くより、私もいいと思います。
取り込みたいと思います。
| if (prefixCount.containsKey(target)) { | ||
| result += prefixCount.get(target); | ||
| } | ||
| prefixCount.put(prefixNum, prefixCount.getOrDefault(prefixNum, 0) + 1); |
There was a problem hiding this comment.
prefixCount.merge(prefixNum, 1, Integer::sum);とも書けるみたいです
There was a problem hiding this comment.
ありがとうございます、claudeがレビューで出してきました。
このmerge関数ですが、初見で何しているかよくわからないなあと思ったので、採用見送りました。
There was a problem hiding this comment.
Goは map[key]++ と書けるのですが、Javaはマップ要素のインクリメントを簡潔に実行する方法なさそうですね
There was a problem hiding this comment.
Go 便利ですね。
hashmap 関係の問題は getOrDefault() を毎回書いてました、少し冗長に感じますよね
|
PR タイトルに問題名を入れていただけると、読み手にとってレビューしやすくなると思います。 |
| ```java | ||
| class Solution { | ||
| public int subarraySum(int[] nums, int k) { | ||
|
|
There was a problem hiding this comment.
ありがとうございます。
そうですね、書き直した時に空いていたようです。修正します。
| int prefixNum = 0; | ||
| int result = 0; | ||
|
|
||
| for(int num : nums) { |
There was a problem hiding this comment.
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
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。
There was a problem hiding this comment.
ありがとうございます。
今まで所属してきたチームでは、if for の前後は空けるでコードを書いてきたので修正します。
|
|
||
| for(int num : nums) { | ||
| prefixNum += num; | ||
| int target = prefixNum - k; |
There was a problem hiding this comment.
complement(補完値、ついになる値)と名付けている方もいらっしゃいました。
There was a problem hiding this comment.
ありがとうございます。
claudeも同様の指摘をしてきたのですが、complementは今回私はあまりしっくりこなかった記憶があります。
| - 問題: [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) | ||
| - 私もしっくりこなかったのでちょっと寝かせてしまった。 |
There was a problem hiding this comment.
わたしもはじめよくわからなかったので、図にしてみました。
https://github.com/h-masder/Arai60/pull/17/changes の560_Subarray_Sum_Equals_K/subarraySum_logic.pngです
There was a problem hiding this comment.
ありがとうございます。
金銭で例えるのはより身近でわかりやすいと思いました。
自分が納得できる例に問題を言い換えるのも、問題を解く上で重要ですね。
問題を解きました、レビューお願いいたします。
560. Subarray Sum Equals K