码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Find the Duplicate Number"

时间:2015-09-29 14:33:25      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

I figured out a O(nlgn) solution, with a sorting. But yes, there are always smarter ones: 

https://leetcode.com/discuss/60830/solutions-explanation-space-without-changing-input-array

class Solution {
public:
    int _findDup(vector<int>& nums, int s, int e)
    {
        if(s == e) return nums[s];

        int mid = s + (e - s) / 2;
        if(nums[mid] < (mid + 1))
        {
            return _findDup(nums, s, mid);
        }
        return _findDup(nums, mid + 1, e);
    }
    int findDuplicate(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        return _findDup(nums, 0, nums.size() - 1);
    }
};

LeetCode "Find the Duplicate Number"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4846174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!