Solution:
Use radix sort. (so it seems not a "hard" level question...)
Code:
class Solution { public: int findDuplicate(vector<int>& nums) { int res = 0, l = nums.size(); for(int i=0; i<l; ++i){ while(i+1 != nums[i]){ if(nums[i] == nums[nums[i]-1]){ res = nums[i]; break; } swap(nums[i], nums[nums[i]-1]); } if(res) break; } return res; } };
No comments :
Post a Comment