Does anyone know how to obtain 0ms in C++?


  • 0
    S

    My code uses O(n) time and no extra space, but the runtime is still 146ms. I see that there are 18.3% of cpp solutions which are 0ms. Is there any more efficient method or the test cases have been changed?

    class Solution {
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums) {
            vector<int> ans;
            for(int i = 0; i < nums.size(); i++){
                if(nums[nums[i] - 1] != nums[i]){
                    swap(nums[nums[i] - 1], nums[i]);
                    i--;
                }
            }
            for(int i = 0; i < nums.size(); i++){
                if(nums[i] - 1 != i) ans.push_back(i + 1);
            }
            return ans;
        }
    };
    

Log in to reply
 

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