Skip to content

Conversation

Nbotter
Copy link
Owner

@Nbotter Nbotter commented May 5, 2025

@Nbotter Nbotter changed the title Leetcode268 Leetcode268 Missing Number May 5, 2025
@huyfififi
Copy link

huyfififi commented May 5, 2025

sortを用いるやり方もあるかなと思うのですが、トライしてみませんか?コミット・プッシュしていただいたらレビューしますよ。

Copy link

@huyfififi huyfififi left a 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:
Copy link

@huyfififi huyfififi May 5, 2025

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に相談すると以下のような提案がなされた
Copy link

Choose a reason for hiding this comment

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

いくつかやり方があると思います。

全部足して全部の和との差を取る。
ソートして下から順番にないものを探す。
全部を set にいれてないものを探す。

ChatGPT に書いてもらうのはいい判断だと思います。

私は、いいなと思うのがでてきたら、何度か同じものを暗記して書くという過程を挟んだほうがよいと思っています。

やった人たちは、
「徹底した違和感の追求」「直感的に納得できて馴染んでいる(はずの)思考のメカニクス的な動かし方を頭の中で繰り返す感覚」「何回も間違える箇所というのはそもそも素直に処理が書けていなかったりして3回連続で通すように書き直すことでコードが整理されますし選択肢の好みも固まっていった」
というようなことを言っています。

arrayを見てそれぞれあっているかを総当たりさせる
arrayの中に存在していたら続けさせて、存在していなかったらその時のiの値を返す
時間計算量:O(n**2)
for文を2重に回しているため
Copy link

Choose a reason for hiding this comment

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

i in nums が回している相当ということですね。

飽きないことが大切なので先に行きましょう。

Copy link

@carolina-museum carolina-museum left a 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
```

Choose a reason for hiding this comment

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

なるほど!!目から鱗の解法ですねΣΣΣ
Discordにいる仲間たちの解法をみて理解するのも役に立つと思うので、もし同じ問題を解いている仲間がいたら参考にしてみるのもおすすめです〜

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