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].

找出满足组成三角形的三条边的个数

思路:组成三角形条件是两条较短的边的和大于第三条边

 1     public int triangleNumber(int[] nums) {
2 Arrays.sort(nums);
3 int count = 0;
4 for (int i = nums.length - 1; i >= 2; i--) {
5 int left = 0, right = i - 1;
6 while (left < right) {
7 if (nums[left] + nums[right] > nums[i]) { //最短的两条边之和大于最长的边nums[i]
8 count += right - left; //left后面的数字,都可以结合right构成三角形
9 right--;
10 } else {
11 left++;
12 }
13 }
14 }
15 return count;
16 }
 

611. Valid Triangle Number的更多相关文章

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

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

  2. **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 ...

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

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

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

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

  5. 611. Valid Triangle Number三角形计数

    [抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...

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

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

  7. Leetcode 之 Valid Triangle Number

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

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

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

  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设置数据库表里的某个字段的数据是唯一的 ALTER TABLE 表名 ADD unique(`表中的字段`)

  2. 『与善仁』Appium基础 — 26、常用手机操作的API说明

    目录 1.获取当前手机的时间 2.获取手机屏幕的宽和高 3.获取手机当前网络 4.设置手机网络模式 5.操作手机通知栏 6.综合练习 7.发送键到设备(掌握) 8.手机截图(掌握) 是针对手机一些常用 ...

  3. c(++)可变参数之格式化字符串

    0.序言 使用printf函数,其参数就是可变参数.下面将使用 C语言  的库函数实现可变参数的函数 . 用途(欢迎补充): A.记录日志,可能需要将变量格式化输出到日志文件. B.格式化字符串,显示 ...

  4. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  5. 【LeetCode】766. Toeplitz Matrix 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...

  6. 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)

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

  7. 【Web前端】css属性cursor注意事项

    注意使用cursor的url时url的括号后面必须+   ,auto: 错误示范:cursor:url('../picture/head.cur'); 正确示范:cursor: url(". ...

  8. MMD

    目录 概 主要内容 定义 MMD for kernel function classes 一个无偏统计量 MMD test Borgwardt K., Gretton A., Rasch M., Kr ...

  9. 更新系统为High sierra 后无法使用Cocoapods

    sudo gem update --system sudo gem install -n /usr/local/bin cocoapods执行完就可以直接用了.

  10. 强强联袂!腾讯云TDSQL与国双战略签约,锚定国产数据库巨大市场

    日前,腾讯云计算(北京)有限责任公司与北京国双科技有限公司签署了<国产数据库产品战略合作协议>,双方将在数据库技术方面展开深度合作,通过分布式交易型数据库的联合研发.产品服务体系建设.品牌 ...