Open
Conversation
t9a-dev
reviewed
Dec 6, 2025
| } | ||
| } | ||
| } | ||
| return result; |
There was a problem hiding this comment.
変数命名について、countなどより意味のある命名にすると分かりやすくなるなと思いました。
t9a-dev
reviewed
Dec 6, 2025
| class Solution { | ||
| public: | ||
| int subarraySum(const std::vector<int>& nums, int k) { | ||
| std::unordered_map<int, int> prefix_count; |
There was a problem hiding this comment.
map系の変数名は key_to_value といった命名が個人的には好みです。(prefix_to_count)変数名を見るとkey,valueにそれぞれどのような意味を持つ値が入るのかが一目でわかるという感覚です。
Owner
Author
There was a problem hiding this comment.
@t9a-dev
ありがとうございます。
今までkey_to_valueみたいな命名を何回か使ってたんですが。map系だからなんですね。すごく分かりやすくなりました。prefix_to_count参考にさせていただきます。
maeken4
reviewed
Dec 13, 2025
| for (int num : nums) { | ||
| prefix_sum += num; | ||
| auto it = prefix_count.find(prefix_sum - k); | ||
| if (it != prefix_count.end()) { |
There was a problem hiding this comment.
C++20以降では要素の存在判定にはstd::map::containsも使えるかと思います。
| class Solution { | ||
| public: | ||
| int subarraySum(const std::vector<int>& nums, int k) { | ||
| std::unordered_map<int, int> prefix_count; |
There was a problem hiding this comment.
prefix_sumが出てきている文脈なので推測はできますが、単にprefix_countだと接頭辞を数える?のように一瞬感じました。prefix sumで一つの専門用語のように思うので、個人的にはprefix_sum_to_countのようなものが好みです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This: https://leetcode.com/problems/subarray-sum-equals-k/description/
Next: https://leetcode.com/problems/maximum-depth-of-binary-tree/description/