[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 ...
随机推荐
- SpringData系列一 Spring Data的环境搭建
本节作为主要讲解Spring Data的环境搭建 JPA Spring Data :致力于减少数据访问层(DAO)的开发量.开发者唯一要做的就是声明持久层的接口,其他都交给Spring Data JP ...
- Java 程序员快速上手 Kotlin 11 招
欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:霍丙乾 近经常会收到一些 "用 Kotlin 怎么写" 的问题,作为有经验的程序员, ...
- angular.js的表格指令
html div.col-sm-12 table.table.table-bordered.table-condensed.table-hover.table-striped.dataTable.no ...
- Ubuntu设置终端相对短路径
这个设置相对实际上是比较简单的.在自己的家目录打开.bashrc 找到PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$' 只需要将w修改为大写W保存, ...
- java面试集锦
HashMap和HashTable的区别 他们都是Map接口的实现类,实现了将唯一键值映射到特定的值上. HashMap没有分类或者排序,它允许一个null和多个null值. HashTable类似于 ...
- 将C-风格字符串用作string对象引用参数
string类定义了一种char*到string的转换功能,这使得可以使用C-风格字符串来初始化string对象. 类型为const引用的形参其中一个属性表明:假设实参的参数类型与引用参数不匹配,但可 ...
- jmeter将参数值写入到指定文件
有时在测试过程中需要将测试过程中生成的参数保存下来,jmeter并没有此类功能,此时,可以 通过beanshell编写代码来实现 思路: 每次请求响应返回后,通过正则表达式获取到需要保存的值,通过Be ...
- 开涛spring3(4.3) - 资源 之 4.3 访问Resource
4.3.1 ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...
- ELK菜鸟手记 (四) - 利用filebeat和不同端口把不同服务器上的log4j日志传输到同一台ELK服务器
1. 问题描述 我们需要将不同服务器(如Web Server)上的log4j日志传输到同一台ELK服务器,介于公司服务器资源紧张(^_^) 2. 我们需要用到filebeat 什么是filebeat ...
- 几大排序思想(由javascript编写)
Hello!我是super喵二~~~今天练了几道面试题,突然觉得好久没有好好归纳 过排序算法了.以前都是用C/java编写排序,这次用js来总结下五大排序算法吧: 1.冒泡排序(常用及常考排序算法): ...