0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LeetCodeをC#で解く1

Last updated at Posted at 2024-11-27

アルゴリズムがよくわからないので、Easyからトライすることにした。

 217. Contains Duplicate
 https://leetcode.com/problems/contains-duplicate/description/

問題の概要:
整数配列に同じ値が少なくとも 2 回出現する場合はtrue、すべての要素が重複しない場合はfalseを返す。

回答:
for文のネストで配列内の総当たりをしようとしたが、時間制限でエラーになる。
他ユーザーの解法をチェック。

・配列をソートしてからforで処理する
ソートしておけば総当たりの必要がない。
Array.Sort(配列名)

・hashsetを用いた重複チェック
hashsetにいれると強制的に重複が排除されるので、配列内の重複チェックに使える。
hashset.count()と[配列].Length で比較

参考サイト
https://qiita.com/kohi7777/items/e6264c960fc2e6980596
https://www.rakuten-tarou.com/introduction-to-hashset-in-c/

【今回の学び】
配列で総当たりをすると、要素数が少ない場合は成功するが、要素数が多くなればなるほど厳しい。
当たり前だけど意識できていなかったので。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?