-
[Programmers] 나머지 한 점 - Swift (통과)Algorithm 2022. 12. 31. 20:00
정사각형이 되려면 x가 총 4개 인데 그중 두개가 동일해야하고, y 4개중에 두개 두개가 동일해야 한다.
그래서 x 끼리 모으고 y 끼리 모은 다음에 1개씩만 남은 좌표를 구하고 x, y 를 모은 다음에 리턴해주었다.
func solution(_ v: [[Int]]) -> [Int] { var ans: [Int] = [] var x: [Int] = [] var y: [Int] = [] for location in v { x.append(location[0]) y.append(location[1]) } let countedSetForX = NSCountedSet(array: x) for location in x { let count = countedSetForX.count(for: location) if count == 1 { ans.append(location) } } let countedSetForY = NSCountedSet(array: y) for location in y { let count = countedSetForY.count(for: location) if count == 1 { ans.append(location) } } return ans }
'Algorithm' 카테고리의 다른 글
[Leetcode] 1578. Minimum Time to Make Rope Colorful - Swift 1차 시도 (탈락) (0) 2022.12.30 [Leetcode] 1375. Number of Times Binary String Is Prefix-Aligned - Swift (통과) (0) 2022.12.30 [백준] (Swift) 1697번: 숨바꼭질 - BFS 문제 (0) 2022.05.09 [Programmers] 정렬 : 가장 큰 수 (0) 2022.01.27 [Programmers] 정렬 : K번째수 (0) 2022.01.27