Skip to content

560. Subarray Sum Equals K#16

Open
hemispherium wants to merge 1 commit into
mainfrom
560-subarray-sum-equals-k
Open

560. Subarray Sum Equals K#16
hemispherium wants to merge 1 commit into
mainfrom
560-subarray-sum-equals-k

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 8, 2026
@@ -0,0 +1,8 @@
### step1
累積和の差がkになる部分をカウントすればいいところまでは思いついたが、うまく実装できなかったのでChatGPTに聞いてsumを管理しながらそれまでの累積和からsum - 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

Choose a reason for hiding this comment

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

累積和の差がkになればsubarrayの和がkになるというところまではわかったのですが、どうすべてを走査するのかが思いつきませんでした。sumにnumsの和を足していきながら、それまでのprefixSumCount[sum - k]を足していけば前から順番にうまく数えられるとあとでわかりました。

int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
int count = 0;
int sum = 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.

個人的には、sumprefixSumでもいいのかなと思います。

}
return count;
}
}; No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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

Choose a reason for hiding this comment

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

map や unordered_map は、(キー)_to_(値) という命名をよく見かけます。キーと値にどのような新貝含まれているかが分かりやすいと思います。

public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
int count = 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.

std::count と名前が被ってしまうのではないか、とややもやっとしました。自分なら num_subarrays とすると思います。

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

Choose a reason for hiding this comment

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

lower_snake で命名するのも良く見かけます。この辺りは所属するチームの平均的な書き方に合わせることをお勧めいたします。

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

https://google.github.io/styleguide/cppguide.html#Variable_Names

The names of variables (including function parameters) and data members are snake_case (all lowercase, with underscores between words).

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

prefixSumCount[0] = 1;
for (int num : nums) {
sum += num;
if (prefixSumCount.find(sum - k) != prefixSumCount.end()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

contains() を使用したほうがシンプルになると思います。
https://cpprefjp.github.io/reference/unordered_map/unordered_map/contains.html

if (prefixSumCount.contains(sum - k)) {

@liquo-rice liquo-rice mentioned this pull request May 8, 2026
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