1. Two Sum
1. Two Sum
Given an array of integers, return indices of the two numbers
such that they add up to a specific target.
such that they add up to a specific target.
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
and you may not use the same element twice.
Example:
/**
/**
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(let i=0; i<nums.length;i++){
for(let j=0; j<nums.length; j++){
if((nums[i]+nums[j]==target) && (i!=j)){
var ans;
ans=[i,j];
return(ans);
}else{
continue;
}
}
}
};
思路:
兩層迴圈去找兩兩相加符合且數字不重複的
留言
張貼留言