class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int n = nums.size() - 1;
        int l = 1, r = n;
        while (l < r){
        	int mid = l + r >> 1;
        	int cnt = 0;
        	for (auto x : nums)
        		if (x >= l && x <= mid)
        			cnt++;
        	if (cnt > mid - l + 1) r = mid;
        	else l = mid + 1;
        }
        return r;
    }
};

上述代码中
for (auto x : nums)
作用就是迭代容器中所有的元素,每一个元素的临时名字就是x,等同于下边代码
for (vector<int>::iterator iter = nums.begin(); iter != nums.end(); iter++)

相关文章:

  • 2022-02-01
  • 2022-12-23
  • 2021-11-02
  • 2021-12-08
  • 2021-12-07
  • 2022-12-23
  • 2022-02-18
  • 2021-09-06
猜你喜欢
  • 2022-01-16
  • 2022-12-23
  • 2021-08-14
  • 2021-09-15
  • 2022-12-23
  • 2021-07-17
相关资源
相似解决方案