标签:info inf size nbsp family div mic 不为 __name__
思路:
想到:面积不为0,即能组成三角形,面积就不会是0,即满足两边之和大于第三边。
1、将数组A降序排列,三个指针:up,mid,low分别指向前三个元素;
循环结束条件是:low=len(A);
2、若满足两边之和大于第三边,即up<mid+low,则返回up+mid+low;
3、不满足up<mid+low,则三指针同步后移一位。
1 class Solution(object): 2 def largestPerimeter(self, A): 3 """ 4 :type A: List[int] 5 :rtype: int 6 """ 7 # 在原址上降序排列 8 A.sort(reverse=True) 9 up, mid, low = 0, 1, 2 10 while low < len(A): 11 # 满足两边之和大于第三边,返回周长 12 if A[up] < A[mid] + A[low]: 13 return A[up] + A[mid] + A[low] 14 # 否则三指针后移 15 else: 16 up, mid, low = mid, low, low + 1 17 return 0 18 19 20 if __name__ == ‘__main__‘: 21 solution = Solution() 22 print(solution.largestPerimeter([3, 6, 2, 3]))
标签:info inf size nbsp family div mic 不为 __name__
原文地址:https://www.cnblogs.com/panweiwei/p/12712688.html