【题目描述】Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
看题解前:
1.构成三角形的条件:任意两边之和大于第三边。
2.暴力搜索,穷举出所有的3条边组合,对穷举出的所有组合判断是否满足形成三角形的条件。
看题解后:
1.利用了二分查找去查找,这里附一个二分查找讲得很详细的博客:
http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html
2.利用二分查找找到小于最小两边之和的最大的数并且记录下标为upbound,那么从两条最小边中较大那条边之后的边到upbound之间的数都满足条件。