Markdown

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.
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
Example:
/**
 * @param {number[]} nums
 * @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;
            }
        }
    }
};


思路:
兩層迴圈去找兩兩相加符合且數字不重複的

留言