Skip to content

Conversation

@Ryotaro25
Copy link
Owner

@Ryotaro25 Ryotaro25 commented Jul 29, 2025

問題へのリンク
https://leetcode.com/problems/contains-duplicate/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
152. Maximum Product Subarray

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、sort.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

bool containsDuplicate(vector<int>& nums) {
set<int> unique_nums;
for (auto num : nums) {
if (unique_nums.contains(num)) {

Choose a reason for hiding this comment

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

C++20 の新機能 以前のstd::find()を使った方法もありますかね。
142の問題で、重複判定でも使用されていたので仕様は問題ないと思いますが、
念の為示しておきます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます🙇
単に存在するかどうかの場合はcontainsをよく使います。

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> seen;

Choose a reason for hiding this comment

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

seen という変数名は、少しわかりずらいように思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

unique_numsとしておりましたが、uniqueであること、numsが格納されていることは明らかなので既に見たものとしてseenを使っておりました。
seen_numsやvisited(グラフ問題っぽい?)などはいかがでしょうか?🙇

Choose a reason for hiding this comment

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

すいません。私のコメントが言葉足らずでした。

このくらいの長さのコードでしたら、seenでも分かると思うんです。
ただ、勉強会の趣旨としては「専門家の8割9割ができる常識」を養うですよね。
https://discord.com/channels/1084280443945353267/1237649827240742942/1240307605314867321

私は初心者なので、seen_numsやvisitedのいずれがいいかの判断はできないのですが、(ごめんなさい)
ただ意識して書かれてますか?という感覚でコメントしました。

他の方のコメントで、命名する時の参考があったので、下記に貼っておきます。
一緒に頑張っていきましょう!
https://source.chromium.org/search?q=char&ss=chromium%2Fchromium%2Fsrc

Copy link

Choose a reason for hiding this comment

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

私は、seen くらいでいいかと思っています。
何も知らない人が上から読んでいって、

  bool containsDuplicate(vector<int>& nums) {
    set<int> seen;

この seen の役割はなにか想像がつきますか。その想像裏切られないですよね。よく分かるならば短いほうがいいんです。

コメント集に結構あります。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l

Choose a reason for hiding this comment

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

なるほど。。。確かにsetからのseenの役割を考えると、すぐ想像がつくものなので難しく考えることもなかったかもしれないです。seenNums, numsSetくらいに考えてましたが、短くて分かるならそれでいいですね。もう少し素直になります。

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