-->

Sunday, January 15, 2017

287. Find the Duplicate Number

https://leetcode.com/problems/find-the-duplicate-number/

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