Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions contains-duplicate/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
let numbericSet = Set(nums)
return numbericSet.count < nums.count
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set에 nums를 먼저 담고, 길이를 비교하는 솔루션은 생각 못해 봤는데, 좋은 방법인 것 같습니다.

저는 Set에 nums를 하나씩 담으면서 이미 담긴 num이 있다면, 그 때 return하도록 했는데요!
시간복잡도는 O(N)으로 같겠지만, 처음에 거를 수도 있다는 부분도 참고해 보시면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오 early return 할 수도 있겠네요 ㅎㅎ 좋은 의견 감사합니다 🥰

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early return 명칭 너무 좋네요.
새로운거 알아갑니다.ㅎㅎ

}
}
16 changes: 16 additions & 0 deletions two-sum/doitduri.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
(dict 없이 더 최적화 할 수 있는 방법은 없을까요??)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

딕셔너리는 key-value 자료구조로 조회 시 O(1)인 장점을 이용해서 풀었는데요~,
지금 당장 생각나는 다른 풀이는. .. nums의 최대값 만큼의 array를 생성해서 동일한 로직으로 접근해볼 것 같습니다🙄 ㅠ
대신 공간복잡도가 증가한다는 단점이 있을 것 같습니다. 다양한 관점에서 의견 주셔서 감사합니다 ㅎㅎ

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
let dict = nums.enumerated().reduce(into: [Int: Int]()) { initialValue, num in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce 를 사용해서 Dictionary를 만들 수 있는건 처음 알았네요!
기억이 맞다면 map으로는 불가능 했어서 무조건 반복문 돌리면서 dictionary를 만들었는데,,,

새로운거 배워갑니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

허허.. 저는 반복문이 그렇게 싫더라구요(?)
다만 알고리즘 문제 풀이에서 고차 함수를 사용 할 때 시간복잡도를 유의해서 사용해야할 것 같아요~

initialValue[num.element] = num.offset
}

for startIndex in 0..<nums.count {
let findValue = target - nums[startIndex]
if let endIndex = dict[findValue], startIndex != endIndex {
return [startIndex, endIndex]
}
}

return []
}
}