C++ solution no space O(n) runtime


  • 0
    W
    class Solution3 {
    public:
    	vector<int> findDisappearedNumbers(vector<int>& nums) {
    		for (auto i : nums) {
    			if (i % nums.size() == 0) {
    				nums[nums.size() - 1] += nums.size();
    			} else {
    				nums[i % nums.size() - 1] += nums.size();
    			}
    		}
    		for (auto it = nums.begin(); it != nums.end(); ++it) {
    			if (*it <= nums.size()) {
    				*it = it - nums.begin() + 1;
    			}
    		}
    		for (auto it = nums.begin(); it != nums.end(); ++it) {
    			if (*it > nums.capacity()) {
    				it = nums.erase(it);
    				--it;
    			}
    		}
    		return nums;
    	}
    };
    

Log in to reply
 

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