1.数组越界
ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001a0 at pc 0x00000034c47f bp 0x7ffe8f2eeb70 sp 0x7ffe8f2eeb68
原因:
应为数组越界,AddressSanitizer为数组越界检查工具。
修改循环
2.数组长度
int n = nums.size();
完整代码
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int tem = nums.size();
int i,j;
for(i=0;i<tem-1;i++){
for(j=i+1;j<tem;j++){
if(nums[i]+nums[j]==target){
return{i,j};
}
}
}
return{i,j};
}
};