Simple c++ implementaions


  • 0
    X
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int missingNum = nums.size();
            for (int i = 0; i < nums.size(); i++)
            {
                missingNum += (i - nums[i]);
            }
            
            return missingNum;
        }
    };
    

    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int missingNum = nums.size();
            for (int i = 0; i < nums.size(); i++)
            {
                missingNum ^= (i ^ nums[i]);
            }
            
            return missingNum;
        }
    };
    

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

Log in to reply
 

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