for (auto x : nums)


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++)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM