Skip to content

Create IntersectionOfTwoArrays.md#10

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

Create IntersectionOfTwoArrays.md#10
kt-from-j wants to merge 1 commit intomainfrom
IntersectionOfTwoArrays

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
349. Intersection of Two Arrays
次に解く問題:
929. Unique Email Addresses

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.

お疲れ様です、全体的に読みやすかったです

while (i < uniqueNums1.length || j < uniqueNums2.length) {
if (uniqueNums1[i] == uniqueNums2[j]) {
intersection.add(uniqueNums1[i]);
i ++;
Copy link

Choose a reason for hiding this comment

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

Javaにあまり詳しくないのですが、インクリメント演算子と変数の間にスペースを空けるのは一般的なのでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

一般的ではないですね。。。
自分でも何故スペースを入れたのか分からないです。
ありがとうございます。

List<Integer> intersection = new ArrayList<>();
int i = 0;
int j = 0;
while (i < uniqueNums1.length || j < uniqueNums2.length) {
Copy link

Choose a reason for hiding this comment

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

こちら&&にしないと配列外参照でエラーになりました

Suggested change
while (i < uniqueNums1.length || j < uniqueNums2.length) {
while (i < uniqueNums1.length && j < uniqueNums2.length) {

Copy link
Owner Author

Choose a reason for hiding this comment

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

失礼しました。仰る通り&&が正解です。
お恥ずかしい限りです。

Comment on lines +69 to +72
int[] uniqueNums1 = Arrays.stream(nums1).distinct().toArray();
int[] uniqueNums2 = Arrays.stream(nums2).distinct().toArray();
Arrays.sort(uniqueNums1);
Arrays.sort(uniqueNums2);
Copy link

Choose a reason for hiding this comment

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

以下のように一行で書いてもよいかなと思いました

Suggested change
int[] uniqueNums1 = Arrays.stream(nums1).distinct().toArray();
int[] uniqueNums2 = Arrays.stream(nums2).distinct().toArray();
Arrays.sort(uniqueNums1);
Arrays.sort(uniqueNums2);
int[] uniqueNums1 = Arrays.stream(nums1).sorted().distinct().toArray();
int[] uniqueNums2 = Arrays.stream(nums2).sorted().distinct().toArray();

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご提案いただいた書き方の方がスマートですね。
distinct()があるくらいならsorted()もあるはずと書きながら気づくべきでした。
ありがとうございます!

intersection.retainAll(Arrays.stream(nums2).boxed().toList());
return intersection.stream().mapToInt(Integer::intValue).toArray();
}
}
Copy link

Choose a reason for hiding this comment

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

Set<Integer> set = Arrays.stream(nums1).boxed().collect(Collectors.toSet());
return Arrays.stream(nums2).filter(set::contains).distinct().toArray();

とかですかね。まあ、ループを素朴に回したほうが読みやすい気がします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

StreamでSetに加工することもできましたね。
私も素朴にループを回した方が読みやすい気がします。。。

public int[] intersection(int[] nums1, int[] nums2) {
List<Integer> intersection = new ArrayList<>(Arrays.stream(nums1).distinct().boxed().toList());
intersection.retainAll(Arrays.stream(nums2).boxed().toList());
return intersection.stream().mapToInt(Integer::intValue).toArray();
Copy link

Choose a reason for hiding this comment

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

行数は少ないですが、一行の情報量が多いので読みづらいですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうですね。行数が増えても素朴にループを回す方が短時間で理解しやすいコードになる気がします。
コメントありがとうございます!

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.

4 participants