老师,询问一下为什么这样结果数组只有一个下标这是为什么啊?
来源:7-4 LeetCode:1. 两数之和

weixin_慕丝2377090
2021-09-14
var twoSum = function(nums, target) {
const map = new Map();
const res = [];
for(let i = 0; i < nums.length; i++){
const n = nums[i];
const n2 = target-n;
if(map.has(n2)){
const val = map.get(n2);
res.push(val); //用结果数组接收符合值的下标
}else{
map.set(n, i);
}
}
return res;
};
写回答
1回答
-
lewis
2021-09-14
你需要设置一对进去,而不是一个数的下标
012021-09-14
相似问题