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?

More than 1 year has passed since last update.

LeetCode備忘録_その2_Arrays & Hashing_Contains Duplicate

Posted at

概要

  • LeetCodeを始めました。1テーマごとに解法と学びをメモします
  • 今回テーマは「Arrays & Hashing_Contains Duplicate」

テーマ詳細

原文

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:

Input: nums = [1,2,3,1]
Output: true
Example 2:

Input: nums = [1,2,3,4]
Output: false
Example 3:

Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true

Constraints:

1 <= nums.length <= 105
-109 <= nums[i] <= 109

日本語訳

※G翻訳
整数配列 nums を指定すると、配列内に値が少なくとも 2回出現する場合は true を返し、すべての要素が異なる場合は false を返します。

例 1:

入力: 数値 = [1,2,3,1]
出力: true
例 2:

入力: 数値 = [1,2,3,4]
出力: false
例 3:

入力: 数値 = [1,1,1,3,3,4,3,2,4,2]
出力: true

制約:

1 <= nums.length <= 105
-109 <= nums[i] <= 109

解法

class Solution(object):
    def containsDuplicate(self, nums):
        return len(nums) != len(set(nums))

結果

  • Runtime:420ms
  • Beats 82.84% of users with Python

学び

  • リストをset型(集合型)に変換することでリスト内で重複する要素は削除される。(例:[1,1,2,3]→[1,2,3])

感想

  • 2つの異なるlist間で重複する要素やユニーク要素を取得するときにsetは使っていたが、今回の利用意図のように型変換することで重複要素が除外されることは初めて知った
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?