题目:

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

分析:

给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。

暴力解决的话可能会超时,我们可以对数组先进行降序排序,这样想一个问题,先选择最大的两个边a,b,如果最小的边c能够和a,b组成三角形,那么比c大的所有边都可以和a,b构成三角形。那么

假定有序数列nums = [9,8,7,6,5,4,3,2]

我们先选择a为9,b为8,c为2,c+b>a三角形成立,而2前面的边均可以和a,b构成三角形,那么此次结果加上(7-1)也就是c的索引减去b的索引,然后在令b为7,也就是下一个元素继续判断。

如果有序数列nums = [9,8,7,6,5,4,3,1]

同样的a为9,b为8,c为1,此时不构成三角形,c的索引减1,也就是判断前一个元素能否和a,b构成三角形,9,8,3成立,所以此次结果加(6-1),直到a遍历到数组倒数第二个元素为止,因为此时边已经不够了。

程序:

class Solution {
public:
int triangleNumber(vector<int>& nums) {
if(nums.size() < ) return ;
sort(nums.begin(), nums.end(), greater<int>());
int res = ;
int n = nums.size();
for(int a = ; a < n-; ++a){
int b = a+;
int c = n-;
while(b < c){
if(nums[b] + nums[c] > nums[a]){
res = res + c - b;
++b;
}
else
--c;
}
}
return res;
}
};

LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)的更多相关文章

  1. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

  2. Leetcode 之 Valid Triangle Number

    611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...

  3. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...

  5. 611. Valid Triangle Number三角形计数

    [抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...

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

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  7. 【leetcode】Valid Triangle Number

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  8. 611. Valid Triangle Number

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

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

    611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...

随机推荐

  1. Paper | Toward Convolutional Blind Denoising of Real Photographs

    目录 故事背景 建模现实噪声 CBDNet 非对称损失 数据库 实验 发表在2019 CVPR. 摘要 While deep convolutional neural networks (CNNs) ...

  2. 消息队列的使用<二>:ActiveMQ的基本使用(Java)

    目录 ActiveMQ 介绍 下载.安装和初次运行 Java上初次使用activeMQ 设置请求属性: 可靠性机制 事务 消息消费方式 receive 监听器: 消息类型 发布/订阅模式 非持久订阅 ...

  3. Knative 初体验:CICD 极速入门

    Knative 社区很早就在讨论用 Tekton 替换 Build 模块的相关事宜.Knative Build 官方已经正式说明不再建议使用 Knative Build 了. 如果你知道 Knativ ...

  4. MySQL5.7安装脚本

    目录结构: install_mysql.sh:安装脚本 my.cnf: MySQL配置文件 mysql--linux-glibc2.-x86_64.tar.gz:MySQL二进制包 以下为目录中的文件 ...

  5. 原生PHP和MYSQL练习登陆验证和查询数据到表格

    直接上代码吧 <?php header("Content-type: text/html; charset=utf-8"); //数据量链接 $conn=mysqli_con ...

  6. 2018-8-10-WPF-checkbox文字下掉

    原文:2018-8-10-WPF-checkbox文字下掉 title author date CreateTime categories WPF checkbox文字下掉 lindexi 2018- ...

  7. C#之初识异步

    什么是异步 举个例子:小明的妈妈让小明烧一壶水,水烧开后要倒进水壶里,同时还需要把家里打扫一下. 小明的操作流程一:烧水---->等待至水烧开----->水倒进水壶里--------> ...

  8. 2.将视图添加到 ASP.NET Core MVC 应用

    在本部分中,将修改 HelloWorldController 类,进而使用 Razor 视图文件来顺利封装为客户端生成 HTML 响应的过程. 当前,Index 方法返回带有在控制器类中硬编码的消息的 ...

  9. CentOS 6.9安装MySQL 5.6 (使用yum安装)

    CentOS 6.9安装MySQL 5.6 (使用yum安装) 移除CentOS默认的mysql-libs [root@test01 srv]# whereis mysqlmysql: /usr/li ...

  10. angular获取时间

    获取24小时制的时间<h1> {{time}} </h1> <script> angular.module('app', []) .controller('ctrl ...