Skip to content

Add 349. Intersection of Two Arrays.md #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add 349. Intersection of Two Arrays.md #13

wants to merge 2 commits into from

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Dec 25, 2024

Comment on lines +105 to +108
if sorted_nums1[index1] == sorted_nums2[index2]:
intersection_set.add(sorted_nums1[index1])
index1 += 1
index2 += 1
Copy link

Choose a reason for hiding this comment

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

ここのところ、同じものが見つかったら、同じ間ポインターを進めるようにすると、
intersection_set は list でいいですね。
https://discord.com/channels/1084280443945353267/1183683738635346001/1188897668827730010

- これも片方がものすごく大きい場合どうするかに相当するとおもった。
- https://github.com/hroc135/leetcode/pull/13/files
- pythonでも同様だが、[変数名に型の情報を不必要に入れるべきでない](https://google.github.io/styleguide/pyguide.html#2144-decision:~:text=names%20that%20needlessly%20include%20the%20type%20of%20the%20variable%20(for%20example%3A%20id_to_name_dict))というスタイルがある
- 今回はintersectionだけだと関数名と同じになるし、intersectionで想定するのが集合な気がするが、返り値はintersectionを保持するリストであるので、型名を入れることは許容範囲に感じた
Copy link

Choose a reason for hiding this comment

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

関数名がintersectionであることから積集合を返すことが容易に想像できるので、ローカル変数名はresでもいいかもしれませんね

Copy link

Choose a reason for hiding this comment

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

res は略語ですが1000件ちょっと使われていますね。result のほうが5000件強で多いは多いです。
https://source.chromium.org/search?q=%22return%20res;%22%20filepath:.*%5C.cc$&sq=
https://source.chromium.org/search?q=%22return%20result;%22%20filepath:.*%5C.cc$&sq=

略語は避けるが慣習として明らかならよい、という話でしょう。

略語は略し方の不一致から、たまに気が付きにくい事故を起こします。たとえば、C++ で継承をしようとして Id と ID を間違えると、意図せずに新たに関数を定義してしまうことになります。
これ自体は C++11 からの override つけておきましょう、ですが、英単語を使うことで綴りが違うことへの違和感をより使おうという話です。

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