1.两数之和/Two sum swift

2018-05-07  本文已影响43人  张三疯疯子

两数之和


classSolution{

    functwoSum(_nums: [Int],_target: Int) -> [Int] {

        let count = nums.count        
        if count <= 0 {
            return []
        }

        var dic: [Int:Int] = [:]

        for i in 0...count-1 {

            let value = nums[i]

            if let b = dic[target-value] {

                return [i,b]

            }

            dic[value] = i

        }

        return []

    }

}

上一篇下一篇

猜你喜欢

热点阅读