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

思路:

首先将数组排序。依次取出前两个边的长度。第三条边长度小于前两个边的和。

查找第三条边用二分查找。

int triangleNumber(vector<int>& a) {
int i,j,k,n,left,right,mid,ans,sum;
ans=;
sort(a.begin(),a.end());
n=a.size();
for (i=;i<n;i++)
for (j=i+;j<n;j++)
{
sum=a[i]+a[j];
left=j;right=n;
while (right-left>)
{
mid=(left+right)/;
if (a[mid]>=sum) right=mid;
else left=mid;
}
ans+=left-j;
}
return ans;
}

参考:

https://leetcode.com/lympanda/

[leetcode-611-Valid Triangle Number]的更多相关文章

  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 611. Valid Triangle Number有效三角形的个数 (C++)

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

  3. Leetcode 之 Valid Triangle Number

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

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

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

  5. **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 ...

  6. 【leetcode】Valid Triangle Number

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

  7. 611. Valid Triangle Number

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

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

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

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

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

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

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

随机推荐

  1. $>_<$

    Hello word! 从jdk环境变量的配置,myeclipse的安装,tomcat的部署和使用,面向对象的编程思想,什么是java. 思维从模糊到清晰,一路摸索,不见泰山!

  2. vue.js应用开发笔记

    看vue.js有几天了,之前也零零散散的瞅过,不过一直没有动手去写过demo,这几天后台事比较少,一直在讨论各种需求(其实公司对需求还是比较重视与严谨的,一个项目需求讨论就差不多一周了,这要搁之前,天 ...

  3. C返回函数指针的函数

    如下函数 char (*retCharWithInt(char, char))(int); 申明了函数指针retCharWithInt,该指针指向一个形参是(char,char),返回值是char(* ...

  4. JavaScript window与undefined作为参数的作用

    1.原函数 输出结果:1 如图: 2.加window的参数 输出结果:window对象 如图: 注意:此时的window不是全局变量,而是局部变量 3.关于形参必须传window么?当然是不需要的 输 ...

  5. 编写第一个python selenium程序(二)

    上节介绍了如何搭建selenium 系统环境,那么本节来讲一下如何开始编写第一个自动化测试脚本. Selenium2.x 将浏览器原生的API封装成WebDriver API,可以直接操作浏览器页面里 ...

  6. 在Eclipse如何实现在xml文件实现代码提示

    通常我们创建xml文件时, 总会在编辑代码的时候不能像编辑Java文件那样进行自动提示或者补全.其实这个是可以实现的,下面我就以struts2.xml进行示范: 1.点击"winbdows& ...

  7. h5分享页面打开APP

    项目中 直播app分享出来的直播h5页面 点击进入按钮:已下载app 就进入app,未下载跳转到下载页面 判断是安卓还是ios var u = navigator.userAgent; var isA ...

  8. Debian系统简要说明

    Debian这个是我最喜欢也是比较熟悉的一个系统了,BD下做个简要说明 一,APT以及dpkg常见用法如下:功能具体语句 软件源设置     /etc/apt/sources.list 更新软件源数据 ...

  9. 使用FileUtils简化你的文件操作

    前言: 在工作当中我们往往遇到很多文件的操作,我们也习惯写一些自己定义的工具类来简化文件操作,其实apache的commons的FileUtils类就是这样一个工具类,使用它能大大的简化我们对文件的操 ...

  10. mysql变量使用总结(转)

    set语句的学习: 使用select定义用户变量的实践将如下语句改成select的形式: set @VAR=(select sum(amount) from penalties);我的修改: sele ...