1. 問題描述
Find a triplet that sum to a given value
Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true.Else return false. For example, if the given array is {12, 3, 4, 1, 6, 9} and given sum is 24, then there is a triplet (12, 3 and 9) present in array whose sum is 24.
給定一個數(shù)組馍忽,判斷是否有三個元素之和為給等的值,如存在返回true臭挽,否則返回false。
2. 解答
這里提供兩種解答方案:
1). Brute force: O(n^3)
2). O(n^2), Sort the array first.