611. Valid Triangle Number**

电子技术——共栅和共源共栅放大器的高频响应 我们在之前学过无论是是CS放大器还是CE放大器,都可以看做是一个带通(IC低通)滤波器。在高频处的响应收到输入电容 $C_{in}$ 的限制(主要是米勒效应)。因此,若想扩大放大器的带宽,我们必须减小米勒效应,一种方案就是共栅放大器。 阅读详情

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 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:

  1. The length of the given array won’t exceed 1000.
  2. The integers in the given array are in the range of [0, 1000].

解题思路

对数组进行从小到大排序, 指针 i 指向最长边, 然后在 [0, i - 1] 范围内搜索 l 和 r 使得 nums[l] + nums[r] > nums[i].

C++ 实现 1

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        if (nums.size() < 3) return 0;
        int count = 0;
        std::sort(nums.begin(), nums.end());
        for (int i = nums.size() - 1; i >= 2; --i) {
            int l = 0, r = i - 1;
            while (l < r) {
                if (nums[l] + nums[r] > nums[i]) {
                    count += r - l;
                    -- r;
                } else ++ l;
            }
        }
        return count;
    }
};

C++ 实现 2

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        if (nums.size() < 3) return 0;
        int count = 0;
        std::sort(nums.begin(), nums.end());
        // 三元组 (i, j, k), 那么 i 最多到 nums.size() - 3
      	// k 的初始值为 i + 2. k 最多移动到 nusm.size() - 1;
      	// 当确定 j 之后, 使用 while 循环知道第一个不满足 > 条件的 k,
      	// 那么满足条件的三角形就是 k - j - 1.
      	// 注意 k = i + 2 放置的位置, 当 j 发生变化时, nums[i+2,...,k-1]
      	// 肯定满足 nums[i] + nums[j] > nums[k] 的, 所以不需要重复累加.
        for (int i = 0; i < nums.size() - 2; ++i) {
            int k = i + 2;
            for (int j = i + 1; j < nums.size() && 
                        nums[i] != 0 &&
                        nums[j] != 0; ++j) {
                while (k < nums.size() && nums[i] + nums[j] > nums[k]) ++k;
                count += k - j - 1;
            }
        }
        return count;
    }
};
GBT 27930-2015/2023国标充电协议A类CAN报文ID说明 国标充电CAN的ID,仅供学习参考。 阅读详情

相关推荐

Proteus快速入门(最详细教程)

Proteus 是一款用于嵌入式单片机仿真的软件,用于在打PCB板之前对软件程序和硬件电路的正确性进行仿真验证。1.所以Proteus一般会和Keil软件进行联合仿真,在Proteus中绘制系统的电路图,在Keil软件中编写对应的程序后生成Hex文件。2.Proteus的仿真结果仅供参考,只能在一定程度上验证系统的可能性。实际实物系统还需要进行进一步的调试。以上即为本次分享的Proteus入门教程的全部内容,希望可以能够帮助到各位道友。

ediykk的博客 15万+

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

芯片设计流程中的def文件

在实际的DEF文件中,每个部分都包含了与实际设计相关的详细信息,这个模板只展示了每个部分的基本结构。在实际应用中,DEF文件通常由EDA(电子设计自动化)工具在完成布局(Placement)和布线(Routing)步骤后自动生成,在集成电路设计中,DEF(Design Exchange Format)文件的模板具体内容会依设计的复杂性和特定需求而变化。简而言之,LEF文件提供了单元的物理特性和规则,而DEF文件使用这些规则来确定单元。)文件描述了特定设计中所有物理元素的精确放置和连线信息。

qq_36045093的博客 4877

Valid Triangle Number (第十六周 数组)

Valid Triangle Number (第十六周 数组)

lin360580306的博客 582

用ESP32-S3+OV2640摄像头+ILI9488屏,DIY一个能联网的智能猫眼(附完整代码)

本文详细介绍了如何利用ESP32-S3微控制器搭配OV2640摄像头和ILI9488显示屏,DIY一个功能强大的智能猫眼系统。从硬件选型、电路设计到软件开发,提供了完整的解决方案和优化技巧,包括图像采集显示、WiFi远程访问及低功耗设计,适合智能家居爱好者动手实践。

weixin_30897233的博客 514

从零构建eSIM RSP服务:云原生架构与GSMA合规实践

本文详细阐述了从零开始构建一个符合GSMA SAS-SM安全标准的云原生eSIM RSP服务的完整路径。内容涵盖云原生基础设施选型、容器化部署、GSMA标准接口集成,并重点分享了通过SAS-SM安全审计的实战经验与避坑指南,为物联网及智能设备企业自建可控的远程SIM配置服务提供了系统性的解决方案。

weixin_29322553的博客 467

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: Input: [2,2,3,4] Output: 3 Explanation: Valid combination

蓝羽飞鸟的博客 301

Windows/Mac/Linux三系统保姆级教程:用llama.cpp将safetensors模型转gguf格式(附常见错误解决)

本文提供Windows、Mac和Linux系统下使用llama.cpp将safetensors模型转换为gguf格式的详细教程,涵盖环境配置、转换原理及常见错误解决方案。通过跨平台兼容性处理,帮助开发者高效完成模型格式转换,优化边缘计算性能。

weixin_29227425的博客 279

有效三角形的个数-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

【ARMv9 DSU-120 系列 1 -- DSU-120 DynamIQ 概述】

ARMv9架构中的DynamIQ™ Shared Unit-120(简称DSU-120)为一组基于Cortex-A类架构的核心提供了共享的L3内存系统、窥探控制(snoop control)和 过滤(filtering) 以及其他支持集群运作的控制逻辑。这个集群被称为DSU-120 DynamIQ™集群。此外,所有连接到片上系统(SoC)的外部接口也都是通过DSU-120提供的。一个DSU-120 DynamIQ™集群可以包含1到14个核心,且同一集群中可包含多达三种不同类型的核心。

CodingCos的博客 1195

【系统分析师】第1章-基础知识:绪论(核心总结)

《系统分析师教程(第二版)绪论》概述了系统分析师的核心角色与知识体系。作为信息系统开发的核心复合型人才,系统分析师需具备技术、业务、管理及道德等多维能力,贯穿需求分析、系统设计、协调沟通和质量保障全流程。知识体系涵盖系统工程方法论(如霍尔三维结构)、信息技术基础(数据库/网络技术)及管理学数学基础。教程梳理了该职业从数据处理专家到现代技术整合者的演变历程,并指出职业发展路径(初级分析师至CTO)。第二版新增大数据/AI等前沿内容,强化案例与实践,是软考官方权威教材,为读者构建系统化的学习框架。

数据知道的博客 6919

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

【AI for Medecine】吴恩达医学图像AI专项课程(诊断、预测、治疗)

近期需要简单了解AI4Science上的一些应用,看到吴恩达大佬的专项课程,于是开始来个“全面”的了解--医学人工智能(诊断、预测、治疗)。

图挖掘领域,新晋砖家 ☞ 未来可期,欢迎和静静一起学习交流吖 795

**611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than...

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_30567471的博客 97

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

【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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[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. Exampl...

a0712104790的博客 291

[LeetCode]611. Valid Triangle Number

https://leetcode.com/problems/valid-triangle-number/#/description 找出满足组成三角形的三条边的个数 组成三角形条件是两条较短的边的和大于第三条边 public class Solution { public int triangleNumber(int[] nums) {

gqk289的专栏 1739

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. 有效三角形的个数 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

FZU 1004 Number Triangle(简单DP)

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the top and ends somewhere on the base. Each step can g

linlinsong—ACM界蒟水! 1118

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

【LeetCode】611. Valid Triangle Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tria...

负雪明烛 2098

leetcode--Valid Triangle Number

问题描述:给定一个非负数组,将它们的大小作为长度,能够在组成多少个合法的三角形。下面是一个例子: 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 分析: (1)这个问题我开始没有想到较好的方法,于是...

weixin_32931837的博客 246

洛谷 数字三角形 Number Triangle C语言

Part0 题目描述 即寻找最优路径并输出所有路径总和 Part1 解决办法 将此问题抽象为完全二叉树,即每一个小二叉树左右节点均确定有值。动态规划问题思路之一:从顶向下思考,从底向上实现。 所以从第四层开始判断: 节点为2时:只有4和5; 节点为7时,只有5和2; 节点为4时,只有2和6; 节点为4时,只有6和5; 依次再考虑上面层,大佬可能已经想到了,就是for嵌套嘛。 Part2 源代码参考: #include<bits/stdc++.h> int data[1005][1005];/

slj1552560的博客 739
上一篇: 454. 4Sum II**
下一篇: 153. Find Minimum in Rotated Sorted Array**
珍妮的选择
博客等级 码龄12年 304粉丝 543原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值