Two Sum - LeetCode
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var answer: [Int] = []
for i in 0 ..< nums.count {
for j in (i + 1) ..< nums.count {
if (nums[i] + nums[j]) == target {
answer.append(i)
answer.append(j)
return answer
}
}
}
return answer
}
}
// TACTIC
// 1. Find two numbers whose sum equals the target using nested for loops.
// 2. Add the two numbers to an array and return that array.
// POINT
// 1. nums.count는 array의 요소의 개수이므로 index와는 항상 차이가 있다.
// 2. target은 i와 j의 합이 아닌 값의 합이다.