개발/Leetcode
Leetcode 217. Contains Duplicate
지산동고라니
2023. 6. 24. 10:43
https://leetcode.com/problems/contains-duplicate/description/
Contains Duplicate - LeetCode
Can you solve this real interview question? 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 Ex
leetcode.com
문제
nums 배열이 주어질 때 중복된 엘레멘트가 하나라도 존재하는 경우 True 그렇지 않은 경우 False를 반환하라
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return False if len(nums) == len(set(nums)) else True
처음에는 list를 만들어 각 원소를 대입하면서 중복체크를 했으나 시간초과가 발생하여 위와같은 코드를 작성함.