-
Notifications
You must be signed in to change notification settings - Fork 0
Leetcode268 Missing Number #3
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
sortを用いるやり方もあるかなと思うのですが、トライしてみませんか?コミット・プッシュしていただいたらレビューしますよ。 |
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.
とりあえずは良いと思います 👍
次からはみなさまと同じく
step 1 -> とにかく解いて思考メモを残す
step 2 -> 自分なりにできるだけ綺麗なコードにする
step 3 -> 3回連続ノーミスでLeetCodeのテストケースをパスするようにする
をするとレビューもしやすいですし、Nbotterさんの成長の近道にもなると思います!
参考までに、手前味噌ですが、私がPR内にどういう情報を残しているのか、参考に置いておきますね。
huyfififi/coding-challenges#11
ちなみにCarolinaさんはかなり詳細に色々書いています。
carolina-museum/coding-challenges#1
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
for i in range(0, len(nums) + 1): | ||
if i in nums: |
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.
if i not in nums:
return i
の方が意図が明瞭ですし簡潔かなと思います。
参考:in
について https://realpython.com/python-in-operator/#pythons-in-operator
return i | ||
``` | ||
|
||
chatgptに相談すると以下のような提案がなされた |
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 にいれてないものを探す。
ChatGPT に書いてもらうのはいい判断だと思います。
私は、いいなと思うのがでてきたら、何度か同じものを暗記して書くという過程を挟んだほうがよいと思っています。
やった人たちは、
「徹底した違和感の追求」「直感的に納得できて馴染んでいる(はずの)思考のメカニクス的な動かし方を頭の中で繰り返す感覚」「何回も間違える箇所というのはそもそも素直に処理が書けていなかったりして3回連続で通すように書き直すことでコードが整理されますし選択肢の好みも固まっていった」
というようなことを言っています。
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.
arrayを見てそれぞれあっているかを総当たりさせる | ||
arrayの中に存在していたら続けさせて、存在していなかったらその時のiの値を返す | ||
時間計算量:O(n**2) | ||
for文を2重に回しているため |
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.
i in nums が回している相当ということですね。
飽きないことが大切なので先に行きましょう。
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.
お疲れ様です🎉🎉🎉🎉🎉
GitHubへの投稿やコードの書き方等、いろいろ慣れるまでは大変なこともあると思うので(私は苦労しました、、)みなさんのアドバイスを全て取り入れて進めるというよりは、慣れたり楽しんだりすることを重点に少しずつ取り入れて進んでいってみてください✨
応援しています!
expected_sum = n * (n + 1) // 2 | ||
actual_sum = sum(nums) | ||
return expected_sum - actual_sum | ||
``` |
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.
なるほど!!目から鱗の解法ですねΣΣΣ
Discordにいる仲間たちの解法をみて理解するのも役に立つと思うので、もし同じ問題を解いている仲間がいたら参考にしてみるのもおすすめです〜
https://leetcode.com/problems/missing-number/description/