原题链接在这里: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. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

  2. 20162326 《Java程序设计》第3周学习总结

    20162326 <Java程序设计>第3周学习总结 教材学习内容总结 这周我通过课堂学习了VIM的列编辑crtl+v,shift+i shift+a·分别是左侧插入和右侧插入.还学习了使 ...

  3. camera corder profile

    /system/etc/ 其中的qulity high 必须与 最大的支持的分辨率相同. 不然cts 不过. 这里的配置必须在报告给app的数据匹配.

  4. java基础(7)--方法

    方法 Math.sqrt() 取平方 Math.pow(2,3) 2的3次方 方法(Method),就是数学函数(Function). 业务方面: 是功能,是动作,一般采用动词命名. 数据层面:是利用 ...

  5. DNS解析过程和DNS挟持

    1.DNS解析过程详解 1).在浏览器中输入一个域名,例如www.tmall.com,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析, ...

  6. Dos命令的巧用 - 转载

    Dos命令的巧用 豪华绚丽的Windows让人们把DOS抛到遥远的记忆角落,然而,真正有价值的东西不会轻易退出历史的舞台.很多人都已经习惯于 Windows的图形化用户界面,熟不知古老的DOS命令却可 ...

  7. Android中获取并设置屏幕亮度

    最近在做一个Demo的时候用到了调节屏幕亮度的功能,于是上网搜索了一下,并且写了一个小Demo测试了一下,发现代码还是比较简单的.Android中的亮度调节,主要有三个方向,一个是针对于系统的亮度调节 ...

  8. Android开发中的logcat工具使用

    http://os.51cto.com/art/200905/126051.htm 用adb直接查看log:    adb logcat 清除之前的log: adb logcat -c 加过滤查看lo ...

  9. Appium 自动化测试(4)-- 脚本开发:官方demo演示 android_contacts.py

    前提:根据前面的环境搭建介绍,安装好相关环境 step1:启动android模拟器 step2:启动Appium服务端 step3:演示代码执行 这里执行的是官方的演示代码:通讯录管理app,安装打开 ...

  10. IOS-界面传值

    第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...