12ms C++ solution


  • -9
    T
    class Solution {
    public:
        int findDuplicate(vector<int>& nums) {
            int i=0,temp=0,count=0;
            while (count!=nums.size()){
                if (nums[i]-1!=i){
                    if (nums[i]==nums[nums[i]-1])
                        return nums[i];
                    else{
                        temp=nums[nums[i]-1];
                        nums[nums[i]-1]=nums[i];
                        nums[i]=temp;
                        count++;
                    }
                }
                else i=nums.size()-1;
            }
            return 0;
        }
    };

Log in to reply
 

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