Conversation
| - 計算量 | ||
| - 時間計算量: O(n) | ||
| - 空間計算量: O(n) | ||
| - これって2回ループ回しているからO(2n)とすべきですか? |
There was a problem hiding this comment.
Big-O記法では定数倍は無視されるためO(n)と書くのが一般的だと思います
https://ja.wikipedia.org/wiki/%E3%83%A9%E3%83%B3%E3%83%80%E3%82%A6%E3%81%AE%E8%A8%98%E5%8F%B7
There was a problem hiding this comment.
定数倍として無視ですよね。
今回改めて考えてBig-O記法って極限での振る舞いを考えるものであるため、nが小さい場合には大雑把過ぎるということが腹落ちしました。
|
|
||
| // 重複を省いたvalの配列を作成 | ||
| ListNode node = head; | ||
| HashSet<Integer> uniqueValManager = new HashSet<>(); |
There was a problem hiding this comment.
Managerという名前は何か管理するための機能を持ったクラスのような印象を受けるので、ここでは少し大げさかなと思いました。
単にuniqueValuesなどしたいのですが、下の配列の名前とちょっと被ってしまいそうですね…
uniqueValuesSet, uniqueValuesListなどにするとかでしょうか。
There was a problem hiding this comment.
ユニークな値を管理するというニュアンスで名付けましたが、変に説明的にし過ぎるよりvisitedなどシンプルな命名の方が直感的ですね。ありがとうございます!
There was a problem hiding this comment.
unique な値を管理するよというのは HashSet を使う時点で自明なので、visited や seen などの用途に着目したシンプルな名前で十分かなと思います。
A collection that contains no duplicate elements.
https://docs.oracle.com/javase/8/docs/api/java/util/Set.html
https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
| } | ||
|
|
||
| // valの配列を元に繋ぎ直す | ||
| ListNode prevNode = head; |
There was a problem hiding this comment.
「前のノード」というのは書き手の視点から見た名前で、これは実際には「重複を排除した新しい連結リスト」を作っていっているのでそのことが分かる名前にされていると読み手にとって分かりやすいかなと思いました。
There was a problem hiding this comment.
新しいLinked listの末尾を指すのでnewTailなどとするのが分かりやすいでしょうか。ありがとうございます!
| } | ||
|
|
||
| ListNode node = head; | ||
| while (node != null && node.next != null) { |
There was a problem hiding this comment.
nodeがnullになるより前に先にnode.nextがnullになるのでwhile (node.next != null)でも動きそうです。
ただそれだと読み手としてちょっと不安(下でnode.valなどにアクセスするときに大丈夫なのかを考えないといけない)ため、個人的にはこちらの書き方のほうが分かりやすいなと思います!
There was a problem hiding this comment.
https://github.com/kt-from-j/leetcode/pull/3/files#r2342761571
で同じコメントしちゃってました 🙏
nanae772さんのコメントも確かにと思う一方、個人的には冗長な条件式はない方が良い気持ちですね…
There was a problem hiding this comment.
冗長なのは承知しているのですが、個人的にはこの書き方が好きです。
とはいえ実務上はコードベースの流儀に合わせてということになると思います。
他の視点を提供していただきありがとうございます!
| } | ||
| ``` | ||
|
|
||
| # step2 他の方の解答を見る |
There was a problem hiding this comment.
参考にした人のリンクとかを貼っておくと、後で見返すときに便利かなと思います。
There was a problem hiding this comment.
確かにその方が便利ですね。
今後リンクを記載するようにしてみます。
ありがとうございます!
| } | ||
|
|
||
| ListNode node = head; | ||
| while (node != null && node.next != null) { |
There was a problem hiding this comment.
ループの中でnodeがnullになることがないので、node != null は不要かもしれません。
| } | ||
|
|
||
| ListNode node = head; | ||
| while (node != null && node.next != null) { |
There was a problem hiding this comment.
https://github.com/kt-from-j/leetcode/pull/3/files#r2342761571
で同じコメントしちゃってました 🙏
nanae772さんのコメントも確かにと思う一方、個人的には冗長な条件式はない方が良い気持ちですね…
|
|
||
| // 重複を省いたvalの配列を作成 | ||
| ListNode node = head; | ||
| HashSet<Integer> uniqueValManager = new HashSet<>(); |
There was a problem hiding this comment.
unique な値を管理するよというのは HashSet を使う時点で自明なので、visited や seen などの用途に着目したシンプルな名前で十分かなと思います。
A collection that contains no duplicate elements.
https://docs.oracle.com/javase/8/docs/api/java/util/Set.html
https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
| prevNode.next = new ListNode(uniqueVals.get(i)); | ||
| prevNode = prevNode.next; | ||
| } | ||
| return head; |
There was a problem hiding this comment.
最初の head だけ既存のものを使い、それ以降は新しい node をつなげているのって何か理由があるのでしょうか?
headも含め、完全に新しい LinkedList として作成する方が自然な気がします。
There was a problem hiding this comment.
headを再利用することで重複削除後のLinkedListの先頭を保持する変数の宣言を省きたかったからだと思います。
とはいえ現状の実装は破壊/非破壊が一貫してなくて見返してみると不自然で気持ち悪いですね。
仰る通り一貫させたほうが自然だと思います。ありがとうございます!
| HashSet<Integer> uniqueValManager = new HashSet<>(); | ||
| ArrayList<Integer> uniqueVals = new ArrayList<>(); |
There was a problem hiding this comment.
new_head と new_node を定義すれば、このあたりの変数はまるっと消せそうな感じがします。
https://github.com/kazizi55/coding-challenges/pull/3/files#diff-153271ae97af52ab3609bb5e1cd3215deb7bc0ef64f6750b2220ed75868283f4
There was a problem hiding this comment.
最初に思い浮かんだのがSetを利用した実装でしたが、Set使わなくても実装できます。
非破壊での実装例のご紹介ありがとうございます!
この問題の非破壊での実装は試してなかったです。参考になります!
今回解いた問題:
83. Remove Duplicates from Sorted List
次に解く問題:
82. Remove Duplicates from Sorted List II