C++ 7 lines count sort O(n)


  • 0
    B
    class Solution {
    public:
        vector<int> findErrorNums(vector<int>& nums) {
            int len = nums.size();
            vector<int>count(len+1,0);
            for(auto e : nums) count[e]++;
            vector<int>ans(2,0);
            for(int i = 1; i <= len; i++)
            {
                if(count[i] == 0) ans[1] = i;
                if(count[i] == 2) ans[0] = i;
            }
            return ans;
        }
    };

Log in to reply
 

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