LeetCode Valid Triangle Number
原题链接在这里:https://leetcode.com/problems/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].
题解:
也是Two Pointers, 构成三角要求三条边成a + b > c. 设nums[i] 为c, 前面的数中选出nums[l]为a, nums[r]为b.
若是nums[l] + nums[r] > c则符合要求,若继续向右移动l, 有r-l种组合都包括num[r]. 所以res += r-l. r左移一位, 之后可能还有符合条件的组合.
若是nums[l] + nums[r] <= c, 则向右移动l.
Time Complexity: O(n^2). sort 用时O(nlogn), 对于每一个c, Two Points用时O(n). n = nums.length.
Space: O(1).
AC Java:
class Solution {
public int triangleNumber(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int res = 0;
Arrays.sort(nums);
for(int i = 2; i<nums.length; i++){
int l = 0;
int r = i-1;
while(l<r){
if(nums[l] + nums[r] > nums[i]){
res += r-l;
r--;
}else{
l++;
}
}
}
return res;
}
}
类似3Sum Smaller.
LeetCode 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 ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
- 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 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- 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
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- LeetCode题解之Valid Triangle Number
1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...
- [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
随机推荐
- python 利用PIL库进行更改图片大小的操作
python 是可以利用PIL库进行更改图片大小的操作的,当然一般情况下是不需要的,但是在一些特殊的利用场合,是需要改变图片的灰度或是大小等的操作的,其实用python更改图片的大小还是蛮简单的,只需 ...
- JDBCTemplate执行增删改查(CDUR)操作
1.JdbcTemplate简介 (1)Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtil. JdbcTemplate可以直接操作数据库, ...
- Java控制语句——分支、循环、跳转
分支语句(if语句,switch语句): 循环语句(for,while,do...while); 跳转语句(break,continue,return): 分支语句(if语句,switch语句) if ...
- Pandas窗口函数
为了处理数字数据,Pandas提供了几个变体,如滚动,展开和指数移动窗口统计的权重. 其中包括总和,均值,中位数,方差,协方差,相关性等. 下来学习如何在DataFrame对象上应用上提及的每种方法. ...
- BZOJ 3698 XWW的难题:有上下界的最大流
传送门 题意 给你一个 $ n*n $ 的正实数矩阵 $ A $ ,满足XWW性. 称一个 $ n*n $ 的矩阵满足XWW性当且仅当: $ A[n][n] = 0 $ 矩阵中每行的最后一个元素等于该 ...
- mybatis学习(2)
select元素. 自定义resultMap,自定义返回. 建表语句如下所示: create table tbl_dept( id ) primary key auto_increment, dept ...
- # IFE前端(2015春)-task2
第一章 JavaScript数据类型及语言基础 期望达成 掌握JavaScript的各种数据类型概念.判断方法 掌握JavaScript函数.对象的概念 掌握字符串.数字.数组.日期等对象的方法 了解 ...
- 分享知识-快乐自己:能使 Oracle 索引失效的六大限制条件
Oracle 索引的目标是避免全表扫描,提高查询效率,但有些时候却适得其反. 例如一张表中有上百万条数据,对某个字段加了索引,但是查询时性能并没有什么提高,这可能是 oracle 索引失效造成的.or ...
- MailJobUtils 发送邮件
import java.util.List; import java.util.Properties; import javax.annotation.Resource; import javax.m ...
- 通过 HTTP 请求加载远程数据(ajax,axios)
1.http://blog.csdn.net/liaoxiaojuan233/article/details/54176798 (Axios(JS HTTP库/Ajax库)) 2.https://ww ...