C++ short solution away from overflow easy to understand

  • 5
    int missingNumber(vector<int>& nums) {
            int find = 0;
            for(int i=0; i<nums.size();i++)
                find += i-nums[i];
            return find+nums.size();

    Every time plus i minus nums[i] to avoid overflow if the size of nums is large

  • 0

    It's a clear solution, but overflow may occur when n is equal to MAX_INT or MIN_INT

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.