LeetCode 169: Majority Element

이원희·2021년 3월 7일
0

📝 PS

목록 보기
62/65
post-thumbnail

문제 풀이

보통 이런 문제에서 HashMap을 사용했는데 문제 조건에 무조건 Array의 절반을 넘기는 수를 구하라했으므로 Array를 정렬하면 무조건 가운데에 정답이 위치한다.

func majorityElement(_ nums: [Int]) -> Int {
    let sortedNums = nums.sorted()
    return sortedNums[sortedNums.count / 2]
}

LeetCode

0개의 댓글