Solution on Swift 3


  • 0
    P
        func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
            var firstIndex = 0
            var secondIndex = 0
            var resultArr = [0, 0]
            
            for (index, value) in nums.enumerated() {
                let firstValue = value
                
                for index2 in (index + 1)..<(nums.count) {
                    let secondValue = nums[index2]
                    
                    let sum = firstValue + secondValue
                    if sum == target {
                        firstIndex = index
                        secondIndex = index2
                        resultArr = [firstIndex, secondIndex]
                        return resultArr
                    }
                }
            }
            
            
            return resultArr
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.