본문 바로가기

알고리즘

[완전탐색] LeetCode contains-duplicate : 중복찾기 - 스위프트

문제 : https://leetcode.com/problems/contains-duplicate/submissions/

 

Contains Duplicate - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

해결방법

Set로 중복체크하고 그 카운트와 중복체크 안한 배열의 카운트를 서로 비교해서 답을 찾았다.

class Solution {
    func containsDuplicate(_ nums: [Int]) -> Bool {
       var setNums = Set(nums)
        if setNums.count == nums.count {
            return false
        }
        return true
    }
}

 

알게된 점 

1.  swift의  sort 시간복잡도는  O(n log n) 단, 일정하지 않다.

Sort정리 zeed님 블로그보고 많은 도움이 됬다. sort에 커밋이 새로 안올라온 것을 보아 

2018년에 바뀐 알고리즘 그대로 이용하는 것 같다.

modified timsort를 사용한다고 한다.

최악의 경우 O(NlogN)의 시간복잡도를 가지고

최적은 O(N)이라고 한다.

따로 정리 필요

-근데 내일 바쁘므로 시간남을때 정리하겠습니다!-

출처 : https://zeddios.tistory.com/648

 

Swift ) Swift Sorting Algorithm

안녕하세요 :) Zedd입니다. 늦었지만.....메리크리스마스~~~~~~~~ 백준의 수 정렬하기 3... 이 문제로 말할 것 같으면... Swift로 맞은 사람이 단 한명도 없는 문제... 암튼 위 문제를 Swift로 대충 풀어보

zeddios.tistory.com

 

반응형