You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
将数组转存为object,数组的 value 作为 object 的 key,数组的下标作为 object 的 value,
然后去使用 for in 遍历 object,只用 in 判断 value 是否在 object 中。
附 in使用说明
var twoSum = function(nums, target) {
let result = {};
let length = nums.length;
for (var i = 0; i < length; i++) {
let difference = target - nums[i];
if (difference in result) {
return [result[difference], i]
}
result[nums[i]] = i;
}
};
The text was updated successfully, but these errors were encountered:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
将数组转存为
object
,数组的 value 作为 object 的 key,数组的下标作为 object 的 value,然后去使用 for in 遍历 object,只用 in 判断 value 是否在 object 中。
附 in使用说明
The text was updated successfully, but these errors were encountered: