返回数组中两个数字的索引的问题
Problem in returning indices of two numbers in array
我有这个问题:
Given an array of integers nums
and an integer target
, return the indices of two numbers that add up to target
.
示例 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
使用下面的代码可以使用 set
和 Map
获取添加到数组中的值
我需要帮助的是return索引
const arr = [{key1: 2}, {key1: 7}, {key1: 11}, {key1: 15}];
const k = 9;
const valueSet = new Set(arr.flatMap((x) => Object.values(x)));
const valueArray = [...valueSet];
valueArray.forEach((v1, i1) => {
for (let i2 = i1 + 1; i2 < valueArray.length; i2++) {
if ((v1 + valueArray[i2]) === k) {
// Return the indices
return valueArray[i2];
}
}
});
您必须解析并找到给出总和的索引组合。
下面的代码可以帮到你
const arr = [{ key1: 2 }, { key1: 7 }, { key1: 11 }, { key1: 15 }];
const k = 9;
let valueSet = new Set(arr.flatMap((x) => Object.values(x)));
let valueArray = [...valueSet];
let indices;
let isFound = false;
// valueArray.forEach((v1, i1) => {
for (let i1 = 0; i1 < valueArray.length && !isFound; i1++) {
for (let i2 = i1 + 1; i2 < valueArray.length && !isFound; i2++) {
if ((valueArray[i1] + valueArray[i2]) === k) {
//Return the Indices
indices = [i1, i2];
isFound = true;;
}
}
}
console.log(indices);
首先,您需要使用键 [{key1: 2},{key1: 7},{key1: 11},{key1: 15}];
从您的数组中获取包含值的数组。然后,如果条件 (nums[i] + nums[j] == target)
=> (7 + 2 == 9)
为真,则循环值数组和 return 索引。
const arr = [{key1: 2},{key1: 7},{key1: 11},{key1: 15}];
const k = 9;
nums = arr.map((o) => {
return Object.values(o)[0]
})
const twoSum = (nums, target) => {
for(let i = 0; i < nums.length; i++){
for(let j = i+1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
return [i, j]
}
}
}
};
console.log(twoSum(nums, k))
也许应该这样做:
function indice1(target, arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] === target) {
return (result = [arr[i], arr[j]]);
}
}
}
return result;
}
function indice2(target, arr) {
let result = [];
for (const i of arr) {
for (const j of arr) {
if (i + j === target) {
return (result = [i, j]);
}
}
}
return result;
}
function indice3(target, arr) {
return arr.reduce((acc, curr, i, arr) => {
let result = [...acc, curr];
for (const i of acc) {
if (i + curr === target) {
result = [i, curr];
arr.splice(1);
break;
}
}
return result;
}, []);
}
提供的答案效果很好。至于为什么你最初的方法不成功的解释:
使用 forEach
遍历数组与使用传统的 for 循环不同,因为数组中的每个元素都会调用回调函数。在 forEach
循环中使用 return
只是跳出 该特定元素的回调 ,而不是整个迭代(因此, return
在 forEach
循环的行为类似于 for (...)
循环中的 continue
。
您可以阅读更多相关信息 here。
const arr = [{ key1: 2 }, { key1: 7 }, { key1: 11 }, { key1: 15 }];
const k = 9;
let valueSet = new Set(arr.flatMap((x) => Object.values(x)));
let valueArray = [...valueSet];
valueArray.forEach((v,ind) => {
let ind2 = valueArray.findIndex(iv => iv === k - v)
ind2 > -1 && console.log([ind,ind2])
})
我有这个问题:
Given an array of integers
nums
and an integertarget
, return the indices of two numbers that add up totarget
.
示例 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
使用下面的代码可以使用 set
和 Map
我需要帮助的是return索引
const arr = [{key1: 2}, {key1: 7}, {key1: 11}, {key1: 15}];
const k = 9;
const valueSet = new Set(arr.flatMap((x) => Object.values(x)));
const valueArray = [...valueSet];
valueArray.forEach((v1, i1) => {
for (let i2 = i1 + 1; i2 < valueArray.length; i2++) {
if ((v1 + valueArray[i2]) === k) {
// Return the indices
return valueArray[i2];
}
}
});
您必须解析并找到给出总和的索引组合。
下面的代码可以帮到你
const arr = [{ key1: 2 }, { key1: 7 }, { key1: 11 }, { key1: 15 }];
const k = 9;
let valueSet = new Set(arr.flatMap((x) => Object.values(x)));
let valueArray = [...valueSet];
let indices;
let isFound = false;
// valueArray.forEach((v1, i1) => {
for (let i1 = 0; i1 < valueArray.length && !isFound; i1++) {
for (let i2 = i1 + 1; i2 < valueArray.length && !isFound; i2++) {
if ((valueArray[i1] + valueArray[i2]) === k) {
//Return the Indices
indices = [i1, i2];
isFound = true;;
}
}
}
console.log(indices);
首先,您需要使用键 [{key1: 2},{key1: 7},{key1: 11},{key1: 15}];
从您的数组中获取包含值的数组。然后,如果条件 (nums[i] + nums[j] == target)
=> (7 + 2 == 9)
为真,则循环值数组和 return 索引。
const arr = [{key1: 2},{key1: 7},{key1: 11},{key1: 15}];
const k = 9;
nums = arr.map((o) => {
return Object.values(o)[0]
})
const twoSum = (nums, target) => {
for(let i = 0; i < nums.length; i++){
for(let j = i+1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
return [i, j]
}
}
}
};
console.log(twoSum(nums, k))
也许应该这样做:
function indice1(target, arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] === target) {
return (result = [arr[i], arr[j]]);
}
}
}
return result;
}
function indice2(target, arr) {
let result = [];
for (const i of arr) {
for (const j of arr) {
if (i + j === target) {
return (result = [i, j]);
}
}
}
return result;
}
function indice3(target, arr) {
return arr.reduce((acc, curr, i, arr) => {
let result = [...acc, curr];
for (const i of acc) {
if (i + curr === target) {
result = [i, curr];
arr.splice(1);
break;
}
}
return result;
}, []);
}
使用 forEach
遍历数组与使用传统的 for 循环不同,因为数组中的每个元素都会调用回调函数。在 forEach
循环中使用 return
只是跳出 该特定元素的回调 ,而不是整个迭代(因此, return
在 forEach
循环的行为类似于 for (...)
循环中的 continue
。
您可以阅读更多相关信息 here。
const arr = [{ key1: 2 }, { key1: 7 }, { key1: 11 }, { key1: 15 }];
const k = 9;
let valueSet = new Set(arr.flatMap((x) => Object.values(x)));
let valueArray = [...valueSet];
valueArray.forEach((v,ind) => {
let ind2 = valueArray.findIndex(iv => iv === k - v)
ind2 > -1 && console.log([ind,ind2])
})