우규이인우윤
Eager To Learn 🌌
우규이인우윤
전체 방문자
오늘
어제

블로그 메뉴

  • 🏡 홈
  • 🚀 깃허브
  • ⛅ 태그 클라우드
  • 분류 전체보기 (217)
    • 👨🏻‍💻 PS (170)
      • JAVA (82)
      • MYSQL (1)
      • Docker (2)
      • PYTHON (24)
      • LeetCode 150 (39)
      • Algorithm 기법 (1)
      • 바킹독 (21)
    • 블로그 이사 (0)
    • Error (1)
    • CS (15)
      • DataBase (2)
      • OS (7)
      • Network (1)
      • Spring (1)
      • 자료구조 (3)
      • Java (1)
    • Learned (7)
      • Spring (7)
    • 개발서적 (15)
      • 가상 면접 사례로 배우는 대규모 시스템 설계 기초 (1)
      • 오브젝트 - 조영호 (7)
      • 친절한 SQL 튜닝 (7)
    • 회고 (2)
hELLO · Designed By 정상우.
우규이인우윤

Eager To Learn 🌌

👨🏻‍💻 PS/PYTHON

[파이썬 PYTHON · 자바 JAVA] LeetCode【Contains Duplicate】

2023. 3. 10. 18:54
 

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


파이썬 코드

class Solution(object):
    def containsDuplicate(self, nums):
        _set = set()
        for num in nums:
            if num in _set:
                return True
            else:
                _set.add(num)
        return False

자바 코드

import java.util.*;
class Solution {
    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set = new HashSet<>();

        for(int i=0;i<nums.length;i++){
            if(set.contains(nums[i])){
                return true;
            }
            set.add(nums[i]);
        }

        return false;
    }
}

 

 

이번 문제는 집합 자료형으로 문제를 해결했다.

 

원리는 Two sum 문제와 같다.

 

원소가 set 자료구조에 입력되어있지 않으면 추가하고, 입력되어 있다면 같은 숫자가 있다는 의미이므로 True를 반환하면 된다.

 

 

 

[파이썬 PYTHON · 자바 Java] LeetCode【Two sum】

Two Sum - LeetCode Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solutio

yinq.tistory.com

 

    '👨🏻‍💻 PS/PYTHON' 카테고리의 다른 글
    • [파이썬 PYTHON · 자바 JAVA] LeetCode【Maximum Subarray】
    • [파이썬 PYTHON · 자바 JAVA] LeetCode【Product of Array Except Self】
    • [파이썬 PYTHON · 자바 JAVA] LeetCode【Best Time to Buy and Sell Stock】
    • [파이썬 PYTHON · 자바 JAVA] LeetCode【Two sum】
    우규이인우윤
    우규이인우윤
    개발자 꿈나무

    티스토리툴바