Given an array of integers and I want to return indices of the two numbers such that they add up to a specific target. Assuming that each input would have exactly one solution, and I haven't used the same element twice.
I am using the brute force approach to Loop through each element x and find if there is another value that equals to target−x
code snippet:
var twoSum = function(nums, target) {
for(var i ; i<nums.length ; i++){
for(var j = i + 1; j<nums.length ; j++){
if (nums[j]==target-nums[i]){
// This is where I want to return new array
return
}
}
}
};
答案 0 :(得分:1)
You just want to return the two indices?
return [i, j];