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中等 ...
随机推荐
- AtCoder Grand Contest 037
Preface 这篇咕了可能快一个月了吧,正好今天晚上不想做题就来补博客 现在还不去复习初赛我感觉我还是挺刚的(微笑) A - Dividing a String 考虑最好情况把每个字符串当作一个来看 ...
- COMP222 - 2019
COMP222 - 2019 - Second CA AssignmentIndividual courseworkTrain Deep Learning AgentsAssessment Infor ...
- 使用k8s-prometheus-adapter实现HPA
环境: kubernetes 1.11+/openshift3.11 自定义metric HPA原理: 首选需要注册一个apiservice(custom metrics API). 当HPA请求me ...
- Vue.js 源码分析(二十五) 高级应用 插槽 详解
我们定义一个组件的时候,可以在组件的某个节点内预留一个位置,当父组件调用该组件的时候可以指定该位置具体的内容,这就是插槽的用法,子组件模板可以通过slot标签(插槽)规定对应的内容放置在哪里,比如: ...
- redis之主从同步
很多企业都没有使用到 Redis 的集群,但是至少都做了主从.有了主从,当 master 挂掉的时候,运维让从库过来接管,服务就可以继续,否则 master 需要经过数据恢复和重启的过程,这就可能会拖 ...
- Mongodb的常用语句
模糊查询 regex(".*?\\"+questionContent+".*") String questionContent = "需求" ...
- Ubuntu关机重启后 NVIDIA-SMI 命令不能使用
问题: 电脑安装好Ubuntu系统后,后续安装了显卡驱动.CUDA.cuDNN等软件,后续一直没有关机.中间系统曾经有过升级,这也是问题所在.系统升级导致内核改变,并可能导致它与显卡驱动不再匹配,所以 ...
- 安装win10和ubuntu双系统
2019-06-22 最近找了一份新的工作,要用到linux,由于之前基本上没有接触过这方面的东西,所以今天捣鼓一下,安装win10和linux双系统,办公研发双不误. 如果在安装的过程中遇到什么 ...
- FPGA最小系统设计
以EP4CE6E22I7为例,设计FPGA最小系统. 程序存储设计 一般使用EPCS4I8N: FPGA_DATA0:13 FPGA_DCLK :12 FPGA_nCS:8 ASDO:6 时钟 待续
- windows下安装pip教程
下载地址是:https://pypi.python.org/pypi/pip#downloads 下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入: python setup.py i ...