本文主要是介绍leetcode-287,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://leetcode-cn.com/problems/find-the-duplicate-number/
思路:二分
int findDuplicate(vector<int>& nums) {int left = 1, right = nums.size() - 1;while(left < right) {int mid = (left + right) / 2;int cnt = 0;for (int i = 0; i < nums.size(); i++) {if (nums[i] <= mid) {cnt += 1;}}if (cnt <= mid) {left = mid + 1;} else {right = mid;}}return left;
}
这篇关于leetcode-287的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!