C++ two methods


  • 1
    X
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            /*
            int n = nums.size();
            int s = 0;
            for(int i = 0;i < n; ++i){
                s += nums[i];
            }
            return (n * (n + 1) / 2) - s;
            */
            int n = nums.size();
            int result = n;
            for(int i = 0;i < n; ++i){
                result ^= i;
                result ^= nums[i];
            }
            return result;
        }
    };

Log in to reply
 

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