Swift Two Pointer O(n) Solution


  • 0
    Z
    func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
            var begin = 0
            var end = numbers.count - 1
            while (begin < end) {
                let sum = numbers[begin] + numbers[end]
                if (sum == target) {
                    break
                } else if (sum < target) {
                    begin += 1
                } else {
                    end -= 1
                }
            }
            return [begin + 1, end + 1]
        }
    }
    

Log in to reply
 

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