알고리즘

[Swift] LeetCode - Missing Number

고고 2022. 1. 11. 14:48

파이썬과 함께하는 자료구조의 이해 연습문제 2장 19번

Leetcode 출처 : https://leetcode.com/problems/missing-number/

 

Missing Number - 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

 

 

class Solution {
    func missingNumber(_ nums: [Int]) -> Int {
        let numsCount = nums.count
        
        let currentSum = numsCount * (numsCount + 1) / 2
        let numsSum = nums.reduce(0, +)
        return currentSum - numsSum
    }
}

XOR로 푸는 방법도 있지만 귀찮...