C++ 代码
class Solution {
public:
int getNumberSameAsIndex(vector<int>& nums) {
if (nums.size()==0) return -1;
bool flag= false;
int res = -1;
for(int i=0;i<nums.size();i++){
if (nums[i] < 0) continue;
else if(nums[i] == i) {
flag = true;
res = i;
}
}
if(flag) return res;
else{
return -1;
}
}
};