LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目:
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].
分析:
给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。
暴力解决的话可能会超时,我们可以对数组先进行降序排序,这样想一个问题,先选择最大的两个边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++)的更多相关文章
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- 611. Valid Triangle Number三角形计数
[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...
- **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 ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
随机推荐
- 用arthas查看JVM已加载的类及方法信息
1.sc:“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息,这个命令支持的参数有 [d].[E].[f] 和 [x:]. [d] 输出当前类的详细信 ...
- 插头Dp总结
T1 HDU1693:Eat the Trees 题目大意:给出n*m的方格,有些格子不能铺线,其它格子必须铺,可以形成多个闭合回路.问有多少种铺法? 插头Dp板子题,题目要求可以是多个回路, 只需要 ...
- 公式推导【IoUNet//ECCV2018】
Jiang B, Luo R, Mao J, Xiao T, Jiang Y. Acquisition of localization confidence for accurate object d ...
- Tomca原理分析之责任链
责任链使用位置:Container处理请求 Container处理请求是使用Pipeline-Valve管道来处理的!(Valve是阀门之意) Pipeline-Valve是责任链模式,责任链模式是指 ...
- APP兼容性测试 (二) 最新 iPhone 机型分辨率总结
iPhone手机发布时间及iOS发布 iPhone是美国苹果公司研发的智能手机系列,搭载苹果公司研发的iOS操作系统. 第一代iPhone于2007年1月9日由苹果公司前首席执行官史蒂夫·乔布斯发布, ...
- windowsServer ------ 安装IIS
1.找到服务器管理器,点击添加角色,一步步执行 2.添加IIS 相关组件 勾选web服务器 下一步 将web服务iis 相关组件全部勾选,ftp 可不选 选择好后安装 等一会 关闭 可以查看到所安装角 ...
- .Net轻松处理亿级数据--clickhouse及可视化界面安装介绍
该篇内容由个人博客点击跳转同步更新!转载请注明出处! 前言 我是在17年就听说过Clickhouse,那时还未接触过亿数据的运算,那时我在的小公司对于千万数据的解决方案还停留在分库分表,最好的也是使用 ...
- 使用jstack排查多线程死锁、阻塞
问题: 针对线上多线程死锁.阻塞,跑着跑着就卡住了 查看线上线程池的状态 jstack用于生成java虚拟机当前时刻的线程快照. 线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成 ...
- Json序列化与反序列化(对象与Json字符串的转换)--C#
public class JsonHelper { #region Json序列化与反序列化 /// <summary> /// 将json转化为对象 /// (需要提前构造好结构一致的M ...
- IIS 503错误解决办法 HTTP Error 503
今天在win7上部署一个IIS网站,莫名出现HTTP Error 503,于是对比了一下之前的网站配置,依然无果. 无奈之下,挨个查看IIS配置.查看“事件查看器”,尝试修改应用程序池 - 高级设置 ...