美文网首页
Arrays-算法练习

Arrays-算法练习

作者: fqyuan | 来源:发表于2017-08-13 16:00 被阅读0次

    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.

    给定一个数组,判断是否有三个元素之和为给等的值,如存在返回true,否则返回false。

    2. 解答

    这里提供两种解答方案:

    1). Brute force: O(n^3)

    2). O(n^2), Sort the array first.

    相关文章

      网友评论

          本文标题:Arrays-算法练习

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