题目:

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. 【java】svn显示&#215;

    背景:将客服系统代码赋值到商户子系统中,复制过去后,所有代码svn显示×. 可能:代码直接复制过去只是表面上再maven中显示的代码复制过去,不是真的代码,所以对于svn来讲真的代码删了,又来了一堆新 ...

  2. oracle序列相关

    一. oracle中如何实现一列的规律增长呢(通常是指number类型的列)? 这就需要借助序列来实现了; 1. 什么是序列? 可以理解为序列是一组sql语法创建出来的函数, 该函数中定义     好 ...

  3. checkbox如何判断是否选中

    checkbox在项目中使用的比较多,好多时候需要判断,或者作为某些逻辑的依据. 总结一下,拿到checkbox状态的方法. <label for="checkbox"> ...

  4. JAVA基础系列:Arrays.sort()

    JDK 1.8  java.util.Arrays.class(rt.jar) 1. Collections.sort方法底层就是调用的Arrays.sort方法. 2. Java Arrays中提供 ...

  5. JavaScript forEach() 方法

    JavaScript forEach() 方法  JavaScript Array 对象 实例 列出数组的每个元素: <button onclick="numbers.forEach( ...

  6. .net core linux环境下 System.Data.SqlClient.SqlException: Connection Timeout Expired.

    最近遇到了一个很奇葩的问题,我编写了一个.net core程序读取多个数据库数据源,进行数据同步处理.该程序在windows环境下运行完全正常,但在linux环境下运行报异常,提示 System.Da ...

  7. python3测试网站网速

    一.运行环境 1.Windows 10 2.python 3.8 二.安装第三方库pycurl 1.先安装 pip install wheel 2.在安装pycurl https://download ...

  8. Java基础—实现多线程的三种方法

    Java虚拟机(JVM,是运行所有Java程序的抽象计算机,是Java语言的运行环境)允许应用程序并发地运行多个线程.在Java语言中,多线程的实现一般有以下三种方法: 1.实现Runnable接口, ...

  9. Vue笔记3

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 基于Proxy的小程序状态管理

    摘要: 小程序状态管理. 作者:wwayne 原文:基于Proxy的小程序状态管理 Fundebug经授权转载,版权归原作者所有. 微信小程序的市场在进一步的扩大,而背后的技术社区仍在摸索着最好的实践 ...