[一起来刷leetcode吧][16]--No.611 Valid Triangle Number

本文介绍LeetCode上第611题Valid Triangle Number的解决方案,该题旨在找出数组中能构成三角形的三个数的数量。文章通过示例说明了如何利用排序和双指针技巧高效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇文章是程序自动发表的,详情可以见 这里
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">

这是leetcode的第611题--Valid Triangle Number

  题目 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.   思路 注意只要三角形的最大边小于另两边之和,所以排序后,进行搜索,避免不必要的比较   

show me the code

class Solution:
    def triangleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ct = 0
        nums.sort()
        n = len(nums)
        for i in range(2,n):
            lo , hi = 0,i-1
            while lo < hi:
                if nums[lo] nums[hi]>nums[i]:
                    ct =hi-lo
                    hi-=1
                else:lo =1
        return ct
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值