美文网首页
1.两数之和/Two sum swift

1.两数之和/Two sum swift

作者: 张三疯疯子 | 来源:发表于2018-05-07 15:58 被阅读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 []
    
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:1.两数之和/Two sum swift

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