-
Notifications
You must be signed in to change notification settings - Fork 0
Remove duplicate from sorted list2 #5
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
node.next = node.next.next | ||
else: | ||
node = node.next | ||
return dummy.next |
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.
きれいで分かりやすいなと思いました。
return head | ||
|
||
current = head | ||
value_counter = defaultdict(int) |
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.
こういうやり方もできるんですね。参考になりました。
while current.next and value_counter[current.next.val] > 1: | ||
current.next = current.next.next | ||
if current.next: | ||
current = current.next |
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.
while current
だと分岐が減ってよりスッキリしそうです。
while current:
while current.next and value_counter[current.next.val] > 1:
current.next = current.next.next
current = current.next
else: | ||
node = node.next | ||
|
||
return dummy.next |
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.
個人的にstep1の方が分かりやすいと思いました。
最後の重複が取り除けないからif is_duplicate:
としていると思うので、意味のつながりでいくと while句 + if句 と else句で分かれると思います。でも、if-else句 の方が書き方としてより強いつながりがあるのかなと想定して読んだので、そこのずれが気になりました。
node = dummy | ||
while node.next and node.next.next: | ||
if node.next.val == node.next.next.val: | ||
duplicateval = node.next.val |
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.
変数名の英単語は _ 区切りにすることをお勧めいたします。
https://peps.python.org/pep-0008/#function-and-variable-names
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
https://google.github.io/styleguide/pyguide.html#316-naming
local_var_name
node = dummy | ||
while node.next and node.next.next: | ||
if node.next.val == node.next.next.val: | ||
duplicateval = node.next.val |
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.
while node.next and node.next.next and node.next.val == node.next.next.val:
node.next.next = node.next.next.next
node.next = node.next.next
と、 2 個目以降の重複した要素を削除したあと、 1 個目の要素を削除するという方法もあります。
return dummy.next | ||
``` | ||
|
||
## step2 読みやすくする&他の人の回答を見る |
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.
もう少し色々な回答を探して読んでみたほうがいい気がします。
ここで学習したいことは「他人のコードから意図を読み取ること」で、思ったよりも「色々な書き方があって」、「それぞれが変形で移り変われる」なと感じて欲しいです。
たとえば、
https://discord.com/channels/1084280443945353267/1226508154833993788/1246022270984392724
大変だったら、多分誰かがまとめていると思います。
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode(0) | ||
dummy.next = head |
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.
ここはstep2以降のように、dummy = ListNode(0, head)で1行で書いてしまった方が個人的にはみやすいですね。
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.
すみません、こちらコメントにありました。
return dummy.next | ||
``` | ||
|
||
重複する要素を全て削除しなければいけない場合はハッシュマップだと二周しなければいけないから効率が悪い? |
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.
効率が何を指しているかによる気がするので明確にできると良いかと思います。(ループ数なのか、計算量なのか)
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode(-1,head) |
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.
細かいですが、ListNode(-1, head)
のように引数同士の間はスペースを開けた方が良さそうに思います。
pep8で引数同士の間のスペースの項目はパッと見つけられませんでしたが、多くの例でスペースが開けられています。
https://pep8-ja.readthedocs.io/ja/latest/
問題 https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/