leetcode 611. Valid Triangle Number(有效三角数)

74HC138译码器的原理和使用 A0,A1,A2为3位二进制加权地址输入端口,A2A1A0顺序组成的3位2进制数对应的十进制数x控制使能端口Yx';例如当A2A1A0 = 101 时,其他门输出端全为高电平,只有Y5'为低电平。 Y0-Y7为8个输出端口,其输出电平由A0,A2,A3,E1,E2,E3决定。 阅读详情

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.
Example 1:
Input: [2,2,3,4]
Output: 3
Explanation:
Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3
Note:
The length of the given array won’t exceed 1000.
The integers in the given array are in the range of [0, 1000].

超过100只能用O(N2)复杂度
在数组中找出所有可以组成三角形的组合数

思路:
如果列举所有组合,来判断两边之和是否大于第三边,会出现TLE
所以只能用O(N2)复杂度的算法

可以这样看,先把数组降序或升序排序,比如说升序
把数组最右边也就是最长的边当作c,那么只需要判断c左边的部分中选出的两条边a,b的和是不是大于c
c左边的部分要选出a,b,a<b,那么可以看到如果a+b>c,那么a右边直到b之前的数+b都会>c,因为数组是升序的,a右边的都会大于a
举个例子[2,2,3,4]
c取4,b取c左边的3,a取第一个数2,a+b>c,所以a到b之间的所有边都可以和b,c组成三角形,即a可取第一个和第二个2

那么就转换成了双指针问题,a+b>c,那么它右边直到b之前的个数都是有效组合数,这时可以把b放小,即b所在的指针左移,计算在新的b的条件下有效的三角边个数
同理如果a+b<c, 就需要把a右移,让a更大一些

然后c从大到小遍历即可

注意每次c到一个新的位置,b都要置为c左边的数,a都要重新置0(最小的边)

    public int triangleNumber(int[] nums) {
        if(nums == null || nums.length < 3) {
            return 0;
        }
        
        int n = nums.length;
        int result = 0;
        
        Arrays.sort(nums);
        
        for(int c = n-1; c > 1; c--) {
            int b = c - 1;
            int a = 0;
            while(b > a) {
                if(nums[a] + nums[b] > nums[c]) {
                    result += (b - a);
                    b --;
                } else {
                    a ++;
                }
            }
        }
        
        return result;
    }
如何编写 Skill 实现自动化用例生成 Skill 本质上是一组结构化的指令和工具定义,它告诉 AI 模型在特定场景下应该遵循什么流程、调用哪些工具、输出什么格式的内容。元信息:名称、描述、版本等。指令(Instructions):定义 AI 的行为准则和任务流程。工具(Tools):AI 可以调用的外部能力,如文件读写、代码执行、API 请求等。上下文(Context):预设的输入输出格式或示例。编写一个用于自动生成测试用例的 Skill,核心在于定义清晰的指令流程和输出规范。 阅读详情

相关推荐

硬件学习笔记(器件篇)—— 磁珠与磁环

因为有这些差别,所有经常会让人们觉得他们并不是一类器件,但实际上他们的原理是一样的。我们使用磁珠是为了利用损耗消耗高频能量,所以如果我们理解了磁珠是如何消耗高频能量的,我们就能理解磁珠的工作原理。

y_u_yu_yu_的博客 1万+

Valid Triangle Number (第十六周 数组)

Valid Triangle Number (第十六周 数组)

lin360580306的博客 582

Cadence17.2 > Allegro > 检查PCB Layout信号线组等长

Cadence17.2 > Allegro > 检查PCB Layout等长和配线

DL_62532的专栏 1万+

有效三角形的个数-triangleNumber

题意 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 2) 2,3,4 (使用第二个 2) 2,2,3 注意: 数组长度不超过1000。 数组里整数的范围为 [0, 1000]。 解题思路 三角形定理,两条较小边大于第三边 双指针指向两个较小边 找第三边(ps大于两条小边) 具体代码 class Solution { public int triangleNumb

tangshuai96的博客 717

MCGS触摸屏Modbus TCP通讯实战:从寄存器写入到字符串处理的完整代码解析

本文详细解析了MCGS触摸屏通过Modbus TCP协议实现寄存器写入和字符串处理的完整代码,涵盖单寄存器写入、多寄存器写入及字符串传输等核心技术。文章还提供了MCGS组态环境中的实战配置步骤和常见问题排查方法,帮助开发者快速掌握工业自动化通讯的关键技术。

study的专栏 770

react-window 大数据列表和表格数据渲染组件之虚拟滚动

ReactWindow是一个高性能的React虚拟滚动组件库,专门用于高效渲染大型列表和表格数据。它通过“窗口化”技术,仅渲染用户可见区域的内容,大幅提升性能并减少内存消耗。主要特点包括:支持固定/可变大小列表项、多种布局类型(垂直/水平/网格)、轻量级设计(不足5KB)以及平滑的滚动体验。相比传统渲染方式,它能轻松处理成千上万条数据,并提供自动滚动、自适应容器大小等高级功能。与同类库相比,ReactWindow具有更简洁的API和更优的性能表现,是处理大数据量渲染场景的理想选择。

前端开发-前端技术分享 1143

611. Valid Triangle Number

# 611. Valid Triangle Number标签(空格分隔): leetcode array medium—# 题目Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make trian

分享人工智能学习心得与实践经验,探讨应用场景,见证变革与进步 654

告别外置天线!手把手教你用HFSS仿真设计一个20x40mm的433MHz PCB蛇形天线

本文详细介绍了如何使用HFSS仿真工具设计一个20x40mm的433MHz PCB蛇形天线,解决物联网设备小型化中的天线集成难题。通过对比外置天线与PCB天线的性能差异,深入解析蛇形天线的电磁学原理,并提供从建模到优化的全流程实战指南,帮助工程师实现高效、低成本的微型化天线设计。

weixin_30606461的博客 444

leetcode611: 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. Example 1

sinat_35425429的博客 902

Captcha must be filled out,解决Kaggle新用户注册无法弹出验证提示的问题

Captcha must be filled out

weixin_46713695的博客 5万+

室内无线传播损耗分析与传播模型验证课程设计

室内无线传播损耗分析与传播模型验证课程设计

a_zxswer的博客 191

【LeetCode】611.Valid Triangle Number解题报告

【LeetCode】611.Valid Triangle Number解题报告tags: Array 题目地址:https://leetcode.com/problems/valid-triangle-number/description/ 题目描述:   Given an array consists of non-negative integers, your task is to cou

郝春雨的博客 1880

ThingsBoard PE专业版解决方案技术文档——温度&湿度

温湿度解决方案

weixin_67440240的博客 831

[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. Example 1: ...

weixin_30532973的博客 96

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 611. 有效三角形的个数(Valid Triangle Number)

611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 Java 实现 import java.util.Arrays; class Solution { public int triangleNumber(int[] nums) { ...

weixin_30336061的博客 257

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode 611.有效三角形的个数(Valid Triangle Number)

Leetcode 611.有效三角形的个数 1 题目描述(Leetcode题目链接)   给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 2) 2,3,4 (使用第二个 2) 2,2,3 注意: 数组长度不超过1000。 数组里整数的范围为 [0, 1000]。 2 题解   分析问题,三角形两边之和大于第三边,我们只判断较小的两边之和是否大于第三边就好了,因此先排序,令数组有

qq_39378221的博客 416

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

611. Valid Triangle Number**

611. Valid Triangle Number** https://leetcode.com/problems/valid-triangle-number/description/ 题目描述 Given an array consists of non-negative integers, your task is to count the number of triplets chosen...

珍妮的选择的博客 332

【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. Example 1: ...

lemonmillie的博客 433

Leetcode 611. Valid Triangle Number O(nlogn) NTT

vector<unsigned> rev=[]() { vector<unsigned> Rev(2048,0); for(int i=0; i<2048; i++) Rev[i]=(Rev[i>>1]>>1)|((i&1)<<(10)); return Rev; }(); class Solution { publ...

StarInShadow的博客 305

[LeetCode] 611. Valid Triangle Number

题:https://leetcode.com/problems/valid-triangle-number/ 题目大意 从数组中取3个数,使得 这 3 个元素 可以组成 三角形。 思路 由于 抽取的方式 是 C 方法一 a + b &gt; c &amp;&amp; abs(a - b) &lt; c 上面 形成 三角形的公式。 对于确定的 a、b,我们只需要确定 c的位置。 对数组排序,按顺序...

u013383813的博客 227

Leetcode 611. Valid Triangle Number 有效三角形的个数

题目:给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。示例 1:输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 2) 2,3,4 (使用第二个 2) 2,2,3 注意:数组长度不超过1000。数组里整数的范围为 [0, 1000]。解题思路:先对数组进行排序,优先确定最大的边长,然后寻找第二第三条边的范围。代码实现:使用b...

zhangzhetaojj的博客 866

leetcode 611. Valid Triangle Number | 611. 有效三角形的个数(Java)

题目 https://leetcode.com/problems/valid-triangle-number/ 题解 a, b, c 三指针,无剪枝优化,O(n^3),居然通过了。 思路是,用 a, c 固定首尾,让 b 在 a, c 之间从左至右游荡。 另外有 O(n^2) 的解法,详见:官方题解,本文不再详述。 class Solution { public int triangleNumber(int[] nums) { Arrays.sort(nums);

寒泉 4万+

LeetCode——611. 有效三角形的个数(Valid Triangle Number)[中等]——分析及代码(Java)

LeetCode——611. 有效三角形的个数[Valid Triangle Number][中等]——分析及代码[Java]一、题目二、分析及代码1. 排序 + 双指针(1)思路(2)代码(3)结果三、其他 一、题目 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 2) 2,3,4 (使用第二个 2) 2,2,3 注意: 数组长度不超过1000。 数组里整数的范围为

江南土豆的博客 351

[leetcode] 611. Valid Triangle Number

Description 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. ...

Learning from the mistakes 288

【Leetcode】611. Valid Triangle Number

题目地址: https://leetcode.com/problems/valid-triangle-number/ 给定一个非负数组A[i]A[i]A[i],求其有多少个三元组可以组成三角形的三条边。 思路是双指针。如果数组长度小于333那直接返回000;否则的话将数组从小到大排序,从A[i]A[i]A[i]开始,判断A[0:i−1]A[0:i-1]A[0:i−1]之间有多少个数对的和是大于A[i]A[i]A[i]的,累加起来即可。代码如下: import java.util.Arrays; publi

数学、算法爱好者的博客 255

Leetcode——611. Valid Triangle Number

题目原址 https://leetcode.com/problems/valid-triangle-number/description/ 题目描述 Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array tha...

想当厨子的程序媛 574
上一篇: leetcode 605. Can Place Flowers(能否种花)
下一篇: leetcode 628. Maximum Product of Three Numbers(三个数的最大乘积)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值