原题链接在这里: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:

  1. The length of the given array won't exceed 1000.
  2. 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的更多相关文章

  1. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  2. LeetCode 611. 有效三角形的个数(Valid Triangle Number)

    611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...

  3. Leetcode 之 Valid Triangle Number

    611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...

  4. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

  5. 【LeetCode】611. Valid Triangle Number 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...

  6. LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  7. 【leetcode】Valid Triangle Number

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  8. LeetCode题解之Valid Triangle Number

    1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...

  9. [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

随机推荐

  1. mysql分库分表(一)

    mysql分库分表 参考: https://blog.csdn.net/xlgen157387/article/details/53976153 https://blog.csdn.net/cleve ...

  2. Luogu-5004 专心OI-跳房子(矩阵快速幂)

    Luogu-5004 专心OI-跳房子(矩阵快速幂) 题目链接 题解: 先考虑最朴素的dp 设\(f[i][0/1]\)表示第\(i\)个位置跳/不跳的方案数,则: \[ \begin{cases} ...

  3. 因为swap分区无法启动

    用户启动时停在如下截图

  4. Java EE启示录

    前言 最近的这段时间一直在学习Java EE,刚刚完成了从0到1的蜕变,所以顺便整理一下我所了解到的Java EE,给刚入门学习的新人一些头绪,而所谓“启示录”,就是这个意思. 一.Java EE是什 ...

  5. 如何让Myeclipse已经关闭掉的项目不显示出来

    一.打开Package Explorer视图,在它的右上角有一个向下的三角图标. 2.点击后选择Filters,在弹出的Filter配置窗口中选中"Closed Projects" ...

  6. 从Activity中返回数据

    从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...

  7. python学习笔记(excel+unittest)

    准备先利用之前整理的python自带的unittest框架 整合excel 实现接口自动化测试功能 先看看excel表格设置: 下来是对excel获取的代码: #!/usr/bin/env pytho ...

  8. spring3: Bean的命名与Bean的实例化

    http://jinnianshilongnian.iteye.com/blog/1413857 2.3.1  XML配置的结构 一般配置文件结构如下: <beans> <impor ...

  9. hdu4348区间更新的主席树+标记永久化

    http://acm.hdu.edu.cn/showproblem.php?pid=4348 sb的标记永久化即可,刚开始add和sum没复制过来wa了两发...,操作和原来的都一样,出来单点变成区间 ...

  10. 配置Nginx作为反向代理服务器

    最近在实习公司的开发一个项目,项目是前后端彻底分离的项目,前端项目和后端项目各监听着特定的端口号,显然不是80的通用端口,为了不在地址栏上输入IP+端口号的形式,我们可以使用Nginx作为反向代理服务 ...