LeetCode-Two Sum

2019-04-11  本文已影响0人  CocoaJason

https://leetcode.com/problems/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:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
    func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
        for i in  0..<nums.count{
            for j in i + 1..<nums.count{
                if nums[i] + nums[j] == target{
                    return [i,j]
                }
            }
        }
        fatalError("找不到")
    }
}
print(Solution.init().twoSum([2, 7, 11, 15], 9).enumerated())
EnumeratedSequence<Array<Int>>(_base: [0, 1])
上一篇下一篇

猜你喜欢

热点阅读