题目:https://leetcode.cn/problems/largest-perimeter-triangle/
'''
给定由一些正数(代表长度)组成的数组 nums ,返回 由其中三个长度组成的、面积不为零的三角形的最大周长 。如果不能形成任何面积不为零的三角形,返回 0。
示例 1:
输入:nums = [2,1,2]
输出:5
示例 2:
输入:nums = [1,2,1]
输出:0
提示:
3 <= nums.length <= 104
1 <= nums[i] <= 106
思路:
1.取出nums中的三个最大值
2.判断如果满足三角形条件,则输出周长
3.如果不满足,则去掉最长的,再往下循环
'''
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
l = 0
while len(nums) >=3:
max1 = max(nums)
max1Index = nums.index(max1)
nums.pop(max1Index)
max2 = max(nums)
max2Index = nums.index(max2)
nums.pop(max2Index)
max3 = max(nums)
max3Index = nums.index(max3)
nums.pop(max3Index)
if max1 + max2 > max3 and max1 + max3 > max2 and max2 + max3 > max1 :
l = max1 + max2 + max3
return l
else:
nums.append(max2)
nums.append(max3)
return