标签:
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