Skip to content

Create 560. Subarray Sum Equals K#28

Open
Apo-Matchbox wants to merge 1 commit intomainfrom
560.-Subarray-Sum-Equals-K
Open

Create 560. Subarray Sum Equals K#28
Apo-Matchbox wants to merge 1 commit intomainfrom
560.-Subarray-Sum-Equals-K

Conversation

@Apo-Matchbox
Copy link
Owner

}
}
}
return result;
Copy link

Choose a reason for hiding this comment

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

変数命名について、countなどより意味のある命名にすると分かりやすくなるなと思いました。

class Solution {
public:
int subarraySum(const std::vector<int>& nums, int k) {
std::unordered_map<int, int> prefix_count;
Copy link

Choose a reason for hiding this comment

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

map系の変数名は key_to_value といった命名が個人的には好みです。(prefix_to_count)変数名を見るとkey,valueにそれぞれどのような意味を持つ値が入るのかが一目でわかるという感覚です。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@t9a-dev
ありがとうございます。
今までkey_to_valueみたいな命名を何回か使ってたんですが。map系だからなんですね。すごく分かりやすくなりました。prefix_to_count参考にさせていただきます。

for (int num : nums) {
prefix_sum += num;
auto it = prefix_count.find(prefix_sum - k);
if (it != prefix_count.end()) {
Copy link

Choose a reason for hiding this comment

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

C++20以降では要素の存在判定にはstd::map::containsも使えるかと思います。

class Solution {
public:
int subarraySum(const std::vector<int>& nums, int k) {
std::unordered_map<int, int> prefix_count;
Copy link

Choose a reason for hiding this comment

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

prefix_sumが出てきている文脈なので推測はできますが、単にprefix_countだと接頭辞を数える?のように一瞬感じました。prefix sumで一つの専門用語のように思うので、個人的にはprefix_sum_to_countのようなものが好みです。

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