题目
思路
三角形判断条件:两个小边之和大于第三边,两个大边之差小于第三边
双指针
代码
class Solution:
"""
@param S: A list of integers
@return: An integer
"""
def triangleCount(self, S):
# write your code here
S.sort()
length = len(S)
count = 0
for i in range(length)[::-1]:
target = S[i]
left = 0; right = i - 1
while left < right:
if left < right and (S[left] + S[right]) > target and (target - S[right]) < S[left]:
count += (right - left)
right -= 1
else: left += 1
return count