[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 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].
思路:
首先将数组排序。依次取出前两个边的长度。第三条边长度小于前两个边的和。
查找第三条边用二分查找。
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]的更多相关文章
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- **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 ...
- 【leetcode】Valid Triangle Number
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 611. Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- 611. Valid Triangle Number三角形计数
[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
随机推荐
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- OpenCV探索之路(三):滤波操作
滤波处理分为两大类:线性滤波和非线性滤波.OpenCV里有这些滤波的函数,使用起来非常方便,现在简单介绍其使用方法. 线性滤波:方框滤波.均值滤波.高斯滤波 方框滤波 #include<open ...
- Eclipse Java 关联源码
今天打代码的时候打算看看Java的源码是怎么实现的 没想到还没关联源码 遇到上面的情况只需要关联下源码就可以对着方法按F3查看JAVA的开源代码. 解决上面如下: 找到jdk的安装目录 找到src.z ...
- 第 10 章 MySQL Server 性能优化
前言: 本章主要通过针对MySQL Server(mysqld)相关实现机制的分析,得到一些相应的优化建议.主要涉及MySQL的安装以及相关参数设置的优化,但不包括mysqld之外的比如存储引擎相关的 ...
- 创建发布自己的npm包
我们基于nodejs平台上面的npm上,可以随意下载很多npm安装包.那我们如何创建自己的npm包呢?很简单,废话少说,开始做~ 开始做之前nodejs默认是要安装的,怎么安装自行百度其他教程. 首先 ...
- 移动端页面 iPhone + Safari 页面调试 之 正确查看网络请求的姿势
如题 本文主要将 Safari + iPhone 前端开发调试 之 正确查看网络请求的 姿势 惯例 说下问题场景: 早知道safari(Mac) + iPhone 调试的方便 能解决很多日常调试问题 ...
- esclipse连接mysql数据库
怎样在eclipse开发环境中连接数据库并测试连接是否成功 1)eclipse开发环境里没有集成mysql的驱动,需要从以下地址下载连接驱动程序mysql-connector-java-XX-XX-X ...
- kotlin语言使用初体验(一)
居说谷歌新认的干儿子kotlin极为受宠,隐隐有替代Java在 android平台老大位置的趋势.kotlin有谷歌撑腰,加上自己的底子也厚,再之与Java无缝兼容,将来在流行的编程语言中占有一席之地 ...
- Elasticsearch与Solr
公司之前有个用Lucene实现的伪分布式项目,实时性很差,后期数据量逐渐增大的时候,数据同步一次需要十几小时.当时项目重构考虑到的是Solr和ES,我参与的是Solr技术的预研.因为项目实时性要求很高 ...
- Tomcat 连接池详解
(转) JDBC 连接池 org.apache.tomcat.jdbc.pool 是Apache-Commons DBCP连接池的一种替换或备选方案. 那究竟为何需要一个新的连接池? 原因如下: Co ...