Swift 给定一个整数数组 nums 和一个目标值 targe

2019-05-31  本文已影响0人  Enter_Y
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
    guard nums.count >= 2 else {
        return [0]
    }
    var tempHash: [Int : Int] = [:]
    var result : [Int] = []

    for (i, value) in nums.enumerated() {
        if let index = tempHash[target - value]{
            result.append(index)
            result.append(i)
            return result
        }
        tempHash[value] = i
    }
    return [0]
}
image.png

还可以提高2%,但想象力贫穷了

上一篇 下一篇

猜你喜欢

热点阅读