반응형

https://leetcode.com/problems/missing-number

 

LeetCode - The World's Leading Online Programming Learning Platform

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

 

n까지의 합을 구하고, nums의 모든 합을 빼주면, 사라진 수를 찾을 수 있습니다.

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int s = 0;
        for(int num : nums) s += num;
        return nums.size() * (nums.size() + 1) / 2 - s;
    }
};
반응형

+ Recent posts