Skip to content

Create SubarraySumEqualsK.md#13

Open
kt-from-j wants to merge 1 commit intomainfrom
SubarraySumEqualsK
Open

Create SubarraySumEqualsK.md#13
kt-from-j wants to merge 1 commit intomainfrom
SubarraySumEqualsK

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
560. Subarray Sum Equals K
次に解く問題:
703. Kth Largest Element in a Stream

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

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

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

Comment on lines +18 to +29
int sum = nums[i];
int j = i;
while (true) {
if (sum == k) {
resultCount++;
}
j++;
if (j == nums.length) {
break;
}
sum += nums[j];
}

Choose a reason for hiding this comment

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

このあたりは以下のようにjに関するforループで書くこともできそうです

Suggested change
int sum = nums[i];
int j = i;
while (true) {
if (sum == k) {
resultCount++;
}
j++;
if (j == nums.length) {
break;
}
sum += nums[j];
}
int sum = 0;
for (int j = i; j < nums.length; j++) {
sum += nums[j];
if (sum == k) {
resultCount++;
}
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそちらの方が素直ですね。ありがとうございます!

public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> prefixSumToCount = new HashMap<>();
prefixSumToCount.put(0, 1);
Integer prefixSum = 0;

Choose a reason for hiding this comment

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

Javaの仕様が分からず教えていただければ嬉しいのですが、
上のコードではintを使われていてこちらはIntegerを使われています。
これはIntegerのほうが良い理由があるからそのようにしているということでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Javaの仕様上、intで宣言しておいた方が良いですね。。。
上段でMapを宣言時にIntegerを使っていたので、Mapに書き込むときにboxingが不要になっていいかと思ったのですが誤りです。
※Mapなどは参照型の値しか格納できないのでIntegerで宣言しています

今回指摘いただき確認するまで厳密に理解していなかったのですが、Interger同士の数値演算を行った場合も、unboxing(Integer→int)が行われint同士に変換された上で計算が実行されるようです。
なので今回のコードで言えば計算に使用する変数は全てintで宣言する方が効率が良さそうです。
良いご指摘いただきありがとうございます。

https://docs.oracle.com/javase/specs/jls/se6/html/conversions.html#170983

If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed.

Choose a reason for hiding this comment

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

丁寧なご回答ありがとうございます。

  • Javaではpremitive型(例: int)と参照型(例: Integer)の違いがある
  • premitive型と参照型の間の変換をboxing/unboxingと呼ぶ
  • 今回はintで定義することでIntegerへのboxingが行われることによるパフォーマンスへの影響を懸念されていた
  • しかし実際計算が行われる際はInteger->intのunboxigがまず行われるのでintのままでよかった

ということで理解しました。

こちらこそJavaについて少し理解が深まりました、ありがとうございます。

@5103246
Copy link

5103246 commented Oct 14, 2025

全体的に良いと思います。
intとIntegerの話は自分も勉強になりました。

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