-
Notifications
You must be signed in to change notification settings - Fork 0
217. Contains Duplicate #32
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
| const containsDuplicate = function(nums) { | ||
| const numToCount = new Map() | ||
| for (const num of nums) { | ||
| if (numToCount.get(num) !== undefined) { |
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.
numの存在判定をしているので、 Map.prototype.has() を用いる方が自然だと感じました。
あと今回のケースでは問題になりませんが、numToCountに undefined を入れるような場合値が登録されているのかどうか判別できませんね。
$ node
Welcome to Node.js v22.16.0.
Type ".help" for more information.
> var m = new Map();
undefined
> m.set(1, undefined)
Map(1) { 1 => undefined }
> m.get(1)
undefined
> m.get(2)
undefined
> m.has(1)
true
> m.has(2)
falseそもそも存在しているかしか見る必要がないので、Setを用いる方が自然な気はしますが。
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.
Map.prototype.has() を用いる方が自然だと感じました。
確かに、set関数でundefined以外の値(boolean)を設定することを前提としたコードになっていますね。
ご指摘の通りだと思いました。
そもそも存在しているかしか見る必要がないので、Setを用いる方が自然な気はしますが。
同じ感想を持ちました!
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の解法が一番最初に思いつきやすい・読む人にも意図が伝わりやすいかなと思いました!
| const containsDuplicate = function(nums) { | ||
| const numToCount = new Map() | ||
| for (const num of nums) { | ||
| if (numToCount.get(num) !== undefined) { |
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の解法が一番最初に思いつきやすい・読む人にも意図が伝わりやすいかなと思いました!
| * `*2` | ||
| * 時間計算量: O(N log N) | ||
| * 空間計算量: O(1) | ||
| * Javasciprtにおいては、sort関数はin-placeで行われるため、O(1)の空間計算量で、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.
JavaScriptに明るくなく、軽く調べただけの知識で恐縮ですが、JavaScriptにおいてのsort()はブラウザの実装によって異なるため、時間・空間計算量はJavaScriptという括りでは括れなさそうな気がします。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.
はっきりとしたReferenceは軽く調べた限りでは手に入れられなかったのですが、最近のChromeなどはTimsortを使用しているらしく、空間計算量O(1)を保証できないような気がします。
|
|
||
| ```javascript | ||
| const containsDuplicate = function(nums) { | ||
| for (let i=0; i < nums.length; i++) { |
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://google.github.io/styleguide/jsguide.html#formatting-horizontal-whitespace
On both sides of any binary or ternary operator.
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.
Google Style Guide でスペースについて書いている記載と場所を把握できておりませんでした。
教えてくださり、ありがとうございます。
|
|
||
| ```javascript | ||
| const containsDuplicate = function(nums) { | ||
| const numToCount = new Map() |
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://google.github.io/styleguide/jsguide.html#formatting-semicolons-are-required
Every statement must be terminated with a semicolon.
ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。
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.
こちらもスタイルガイドにおいて推奨されていることを把握できておりませんでした。
ありがとうございます。
問題URL
https://leetcode.com/problems/contains-duplicate/description/
問題文
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Example 1:
The element 1 occurs at the indices 0 and 3.
Example 2:
All elements are distinct.
Example 3:
Constraints: