-
Notifications
You must be signed in to change notification settings - Fork 0
83.Remove Duplicate from Sorted List #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: branch
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # 83.Remove Duplicate from Sorted List | ||
|
|
||
| ## step1 | ||
|
|
||
| 所要時間 約10分 | ||
|
|
||
| 最初出力型を何にすれば良いかわからなかったので迷った。 | ||
|
|
||
| [リンク](https://leetcode.com/problems/remove-duplicates-from-sorted-list/solutions/5380806/beats-100-java-python-c-fully-explained)を参考にしてコードを書いた。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| node = head | ||
| node2 = head.next | ||
| visited = set() | ||
| visited.add(head.val) | ||
| while node2: | ||
| if node2.val in visited: | ||
| node.next = node2.next | ||
| node = node | ||
| node2 = node.next | ||
| else: | ||
| visited.add(node2.val) | ||
| node = node.next | ||
| node2 = node2.next | ||
| return head | ||
| ``` | ||
|
|
||
| ## step2 | ||
|
|
||
| >The list is guaranteed to be sorted in ascending order. | ||
| 上のように書かれているので同じ数字は連続でしか出現しない。そのためsetに入れる必要はなく直前の値のみを参照すれば良い。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| node = head | ||
| node2 = head.next | ||
| visited = node.val | ||
| while node2: | ||
| if node2.val == visited: | ||
| node.next = node2.next | ||
| node2 = node.next | ||
| else: | ||
| node = node.next | ||
| node2 = node2.next | ||
| visited = node.val | ||
| return head | ||
| ``` | ||
|
|
||
| ```python | ||
| if node2.val = visited: | ||
| node.next = node2.next | ||
| node2 = node.next | ||
| ``` | ||
|
|
||
| この部分は下のようにも書ける | ||
|
|
||
| ```python | ||
| if node2.val = visited: | ||
| node2 = node2.next | ||
| node.next = node2 | ||
| ``` | ||
|
|
||
| node.nextをつなげてからnode2を移動させるか、その逆かが異なる。 | ||
| 下の方が見やすい気もする。 | ||
|
|
||
| 次のような記法もあった。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discord の中に問題名をいれて検索するといろいろな過去の集積が出てくるのでそれをおすすめします。 |
||
| <https://leetcode.com/problems/remove-duplicates-from-sorted-list/solutions/5665020/27ms-99-11-easy-solution-pointer> | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| cur = head | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 変数名は原則フルスペルで書くことをお勧めいたします。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. わかりました。ありがとうございます。 |
||
| while cur: | ||
| while cur.next and cur.next.val == cur.val: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 細かい点となり恐縮ですが、
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かにそうですね。自分もそのように感じます。 |
||
| cur.next = cur.next.next | ||
| cur = cur.next | ||
| return head | ||
| ``` | ||
|
|
||
| こちらの方が短く書けてるが、実際に動かしてみると平均的に5msほど遅かった。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LeetCode 上の実行時間は分散が大きいため、あまり気にしないほうがよいと思います。 |
||
| しかし、27msで解ける時もあったので問題に左右されやすいコードなのだろう。原因はわからない。 | ||
|
|
||
| ## step3 | ||
|
|
||
| 一番最後の方法でミスなく三回連続で成功できるまで行った。 | ||
| コードは同じなので示さない。 | ||
| ==を=にしてしまうミスが多くあった。 | ||
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.
step3のコードのように、変数を一つだけ(例えば
node)にしてやれば,だけで済みますし、こちらの方が直感的にわかりやすいと思います