美文网首页
LeetCode-Two Sum

LeetCode-Two Sum

作者: CocoaJason | 来源:发表于2019-04-11 16:03 被阅读0次

    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])
    

    相关文章

      网友评论

          本文标题:LeetCode-Two Sum

          本文链接:https://www.haomeiwen.com/subject/xsztwqtx.html