-
Notifications
You must be signed in to change notification settings - Fork 0
217. Contains Duplicate #72
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
base: main
Are you sure you want to change the base?
Conversation
| bool containsDuplicate(vector<int>& nums) { | ||
| set<int> unique_nums; | ||
| for (auto num : nums) { | ||
| if (unique_nums.contains(num)) { |
There was a problem hiding this comment.
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の問題で、重複判定でも使用されていたので仕様は問題ないと思いますが、
念の為示しておきます。
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seen という変数名は、少しわかりずらいように思います。
There was a problem hiding this comment.
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(グラフ問題っぽい?)などはいかがでしょうか?🙇
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど。。。確かにsetからのseenの役割を考えると、すぐ想像がつくものなので難しく考えることもなかったかもしれないです。seenNums, numsSetくらいに考えてましたが、短くて分かるならそれでいいですね。もう少し素直になります。
問題へのリンク
https://leetcode.com/problems/contains-duplicate/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
152. Maximum Product Subarray
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、sort.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。